简体   繁体   English

c# 如何获得具有多个相同字符的 substring

[英]c# How to get substring with more than one of the same characters

I have the following string, with the variable of s :我有以下字符串,变量为s

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

I want to extract the Model, XPR500 and the Serial, JK0192我想提取 Model、 XPR500和 Serial、 JK0192

I was able to get the model with the following code:我能够使用以下代码获得 model:

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

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

But, I am having difficulty getting the serial value.但是,我很难获得序列值。 I have attempted this code:我尝试过这段代码:

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

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

but it returns但它返回

JK0192,Condition: Good

I am having trouble getting it to get everything from Serial: and the next comma.我无法从 Serial: 和下一个逗号中获取所有内容。

Can anyone see where I am going wrong?谁能看到我哪里出错了?

You can try to use a simple regex:您可以尝试使用简单的正则表达式:

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

Here is a full example:这是一个完整的例子:

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

Hope this will help希望这会有所帮助

You can use an array of string delimiters.您可以使用字符串分隔符数组。 I would use the titles as delimiters.我会使用标题作为分隔符。 I wouldn't use comma because:我不会使用逗号,因为:

  1. Comma is not always a delimiter逗号并不总是分隔符
  2. You will still need to remove the titles您仍然需要删除标题

Here is some code you can try.这是您可以尝试的一些代码。 It uses String.Split instead of solely searching strings manually and parsing substrings它使用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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM