【问题标题】:How to Read from specified substring to next substring如何从指定的子字符串读取到下一个子字符串
【发布时间】:2014-02-18 11:44:24
【问题描述】:

我有一个字符串,其中包含很多文本,其中有像..这样的子字符串

1 . 1 To Airtel Mobile
1 03/MAR/2013 16:06:31 9845070641 05:44 1.80 **
2 04/MAR/2013 10:00:29 9845096416 00:14 0.30 **
3 04/MAR/2013 20:02:35 9845096416 08:12 2.70 **
4 06/MAR/2013 21:20:03 9632176702 00:37 0.30 **
5 09/MAR/2013 11:40:45 9845444042 01:29 0.60 **
6 11/MAR/2013 18:59:08 9900054971 01:14 0.60 **
7 12/MAR/2013 13:43:01 9686568009 03:57 1.20 **
8 13/MAR/2013 17:38:18 7760995045 00:48 0.30 **
9 21/MAR/2013 09:26:20 9845444043 02:47 0.90 **
10 21/MAR/2013 11:02:39 9845444043 00:15 0.30 **
11 22/MAR/2013 18:00:00 9845096416 00:11 0.30 **
Total 25:28 9.30

现在,根据我的要求,我必须将这个子字符串“1 . 1 To Airtel Mobile”之后的行读取到这个子字符串“Total 25:28 9.30”。这是我在 c# 中的代码,它将检查“1 . 1 To Airtel Mobile”出现在字符串中。现在根据我的要求如何读取指定子字符串的行,即“Total 25:28 9.30”。 这是我在 C# 中的代码..

if(currentText.Contains("1 . 1 To Airtel Mobile"))
 {
         using (StringReader reader = new StringReader(currentText))
                    {
                        //
                    }

【问题讨论】:

标签: c# regex string substring


【解决方案1】:

我创建了一个示例项目并使用您给定的输入测试下面的代码,它给出了您需要的结果。

string strStartString = "1 . 1 To Airtel Mobile";
        string strEndString = "Total 25:28 9.30";

        if (strRawData.StartsWith(strStartString) && strRawData.EndsWith(strEndString))
        {
            int startIndex = strRawData.IndexOf("1 . 1 To Airtel Mobile");
            int endIndex = strRawData.IndexOf("Total 25:28 9.30");

            int whereReadingStarts = strStartString.Length;
            int whereReadingStops = endIndex - whereReadingStarts;

            string strDesiredOutput = strRawData.Substring(whereReadingStarts, whereReadingStops);
            textBox1.Text = strDesiredOutput;
        }

【讨论】:

    【解决方案2】:

    我想这应该适合你

    if (currentText.Contains("1 . 1 To Airtel Mobile") && currentText.Contains("Total"))
    {
        int startPosition = currentText.IndexOf("1 . 1 To Airtel Mobile");
        int endPosition = currentText.IndexOf("Total");
    
        string result = currentText.Substring(startPosition, endPosition-startPosition);
        // result will contain everything from and up to the Total line
        }
    

    【讨论】:

    • 谢谢你先生..一个小查询我必须将“总”行也纳入结果..那么该怎么做呢?
    • IndexOf 采用起始位置,我想您可以使用它在 endPosition 索引之后找到下一个newline
    【解决方案3】:

    您可以在此处使用字符串拆分功能来处理通话详细信息。

    var txt ="1 03/MAR/2013 16:06:31 9845070641 05:44 1.80 **";
    string [] split = txt.Split(new Char [] {' '});
    

    //现在你有

    split[0] = 1;
    split[1]="03/MAR/2013";
    split[2]="16:06:31";
    split[3]="9845070641";
    split[4]="05:44";
    split[5]="1.80";
    split[6]="*";
    

    对数据做任何你想做的事。除了通话详情之外,您还需要更多逻辑来处理线路

    【讨论】:

    • 除非他想读取1 . 1 To Airtel MobileTotal 25:28 9.30 之间的所有行。
    • @Prix 是的,我知道这就是为什么我提到他需要更多逻辑来处理这些线路。并且已经给出了呼叫详细信息的解决方案。
    • 是的,我想阅读“1 . 1 To Airtel Mobile”和“Total 25:28 9.30”之间的所有行,而不是拆分您给出的解决方案的每一行..
    【解决方案4】:

    假设您已经拥有所有需要的文本,您可以执行以下操作:

    var lines = str.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
    

    现在只需跳过lines 中的第一项和最后一项。

    【讨论】:

    • 可能是lines = lines.Skip(1).Take(lines.Count - 1)?
    【解决方案5】:

    您可以使用SkipWhile 跳过所有行,直到To Airtel Mobile 标头。然后跳过这个标题。然后用TakeWhile 取下一行直到总行:

    var options = StringSplitOptions.RemoveEmptyEntries;
    var lines = text.Split(new [] { Environment.NewLine }, options)
                    .SkipWhile(s => !s.Contains("1 . 1 To Airtel Mobile"))
                    .Skip(1)
                    .TakeWhile(s => !s.Contains("Total"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-05
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多