我正在尝试对子字符串进行练习,但效果不佳

编程入门 行业动态 更新时间:2024-10-25 02:20:01
本文介绍了我正在尝试对子字符串进行练习,但效果不佳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

代码为:

private void test() { int countFiles = 0; byte[] a; string startTag = "T256=\""; string endTag = "\""; int startTagWidth = startTag.Length; int endTagWidth = endTag.Length; index = 0; w = new StreamWriter(@"c:\test.txt"); while (true) { index = f.IndexOf(startTag, index); string test = f.Substring(index , startTagWidth); if (index == -1) { break; }

startTag是T256 = endTag应该是一个qouta

The startTag is T256= the endTag should be one qouta "

我需要找到在T256和"

I need to find to get anything between T256 and "

例如,如果我的T256 ="Hi Mose",所以我只需要喂Mose

For example if i have T256="Hi Mose" so i need to get only Hi Mose

现在我得到的是T256 =

For now what im getting is T256=

接下来我该怎么办? (到目前为止,该子字符串用于测试,但最终结果中我不需要的是该字符串)

What should i do next ? ( the substring is for testing so far but not what i need in the final result )

danieli

推荐答案

您好,Danieli,

Hi Danieli,

我在库中找到了一些旧代码. 符合您的要求.

I found some old code in my library. It is working fair with your requirement.

private void Form1_Load(object sender, EventArgs e) { string data = "T256=\"Hi Mose\""; string startTag = "T256=\""; string endTag = "\""; string result = GetStringBetween(data, startTag, endTag); } public string GetStringBetween(string data, string StartKey, string EndKey) { if (String.IsNullOrEmpty(data) || String.IsNullOrEmpty(StartKey) || String.IsNullOrEmpty(EndKey)) return String.Empty; if (!data.Contains(StartKey) || !data.Contains(EndKey)) return String.Empty; int ix_start = data.IndexOf(StartKey); int ix_end = data.IndexOf(EndKey, ix_start + StartKey.Length); int ValueStart = ix_start + StartKey.Length; string ret = data.Substring(ValueStart, ix_end - ix_start - StartKey.Length); return ret; }

请尝试.

更多推荐

我正在尝试对子字符串进行练习,但效果不佳

本文发布于:2023-10-25 18:26:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对子   不佳   字符串   效果

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!