我正在尝试对子字符串进行练习,但效果不佳 [英] Im trying to excersize on substring but its not working good

查看:56
本文介绍了我正在尝试对子字符串进行练习,但效果不佳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码为:

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;
        }

请尝试.


这篇关于我正在尝试对子字符串进行练习,但效果不佳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆