【问题标题】:c# How to get substring with more than one of the same charactersc#如何获取具有多个相同字符的子字符串
【发布时间】:2020-01-25 01:25:08
【问题描述】:

我有以下字符串,变量为s

Room: 501, User: John, Model: XPR500 Serial: JK0192, Condition: Good

我想提取模型XPR500 和序列号JK0192

我能够使用以下代码获取模型:

                int pFrom = s.IndexOf("Model: ") + "Model: ".Length;
                int pTo = s.LastIndexOf(" Serial:");

                String model = s.Substring(pFrom, pTo - pFrom);

但是,我很难获得序列值。我已经尝试过这段代码:

                int pFromt = s.IndexOf("Serial: ") + "Serial: ".Length;
                int pToT = s.LastIndexOf(", ");

                string serial = s.Substring(pFromt, pToT - pFromt);

但它会返回

JK0192,Condition: Good

我无法从 Serial: 和下一个逗号中获取所有内容。

谁能看出我哪里出错了?

【问题讨论】:

  • 所以, 似乎有时是一个分隔符,但并非总是如此。为什么不在Model: XPR500 Serial: JK0192 之间?模型和序列之间的那些空格,还是其他字符?另一个字符可能会呈现为空格。也可能是标签在复制粘贴中丢失了?
  • 这就是从其他来源导出数据的方式。型号和序列号之间永远不会有逗号,但在两者之前/之后可能会有更多的逗号
  • 它们之间总是有多个空格吗?这些真的是空间吗?
  • 确实是3个空格,不是制表符
  • 那么你能把字符串分成3个空格吗?

标签: c# char substring


【解决方案1】:

您可以尝试使用简单的正则表达式:

@"Model\:\s([\w\d]*).*Serial\:\s([\w\d]*).*"

这是一个完整的例子:

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"Model\:\s([\w\d]*).*Serial\:\s([\w\d]*).*";
        string input = @"Room: 501, User: John, Model: XPR500   Serial: JK0192, Condition: Good";
        RegexOptions options = RegexOptions.Multiline;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

希望这会有所帮助

【讨论】:

    【解决方案2】:

    您可以使用字符串分隔符数组。我会使用标题作为分隔符。我不会使用逗号,因为:

    1. 逗号并不总是分隔符
    2. 您仍需要删除标题

    这里有一些您可以尝试的代码。它使用String.Split 而不是仅仅手动搜索字符串并解析子字符串

    static void Main(string[] args)
    {
        var s = "Room: 501, User: John, Model: XPR500   Serial: JK0192, Condition: Good";
        s = s.Replace(",", ""); // first, remove all the commas
        var delimiters = new string[] { "Room:", "User:", "Model:", "Serial:", "Condition:" };
        // use a function which doesn't assume the order or inclusion of all the delimiters
        model = getValue(s, delimiters, "Model:");
        serial = getValue(s, delimiters, "Serial:");
        Console.WriteLine($"Model = '{model}'");
        Console.WriteLine($"Serial = '{serial}'");
        Console.ReadLine();
    }
    
    private static string getValue(string s, string[] delimiters, string delimiter)
    {
        // find the index of the beginning of the rest of the string after your sought title
        var index = s.IndexOf(delimiter) + delimiter.Length;
        // split the rest of the string by all the delimiters, take the first item
        return s.Substring(index).Split(delimiters, StringSplitOptions.RemoveEmptyEntries).First().Trim();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2011-12-27
      相关资源
      最近更新 更多