-2

I have this string...

lg-270-110.sh-300-110.hd-180-1.hr-155-61.ea-1403-62.cc-3007-110-110.ch-220-63.ca-3084-64-64

The string is always changing...

How would i get the data between two certain points...

What i need really is to extract the hd-180-1. and the hr-155-61. and remove them from the string

But you see the data isnt always hd-180-1. - It could be hd-171-4. - So i need to delete the data between HD and . programatically

How would i go about doing this?

4

5 回答 5

1

这看起来像是正则表达式的工作

string s = "lg-270-110.sh-300-110.hd-180-1.hr-155-61.ea-1403-62.cc-3007-110-110.ch-220-63.ca-3084-64-64";

s = Regex.Replace(s, @"hd.*?\.", "");
s = Regex.Replace(s, @"hr.*?\.", "");
Console.WriteLine(s);

这是我最喜欢的正则表达式参考

您还可以使用正则表达式来匹配您的模式

string s = "lg-270-110.sh-300-110.hd-180-1.hr-155-61.ea-1403-62.cc-3007-110-110.ch-220-63.ca-3084-64-64";

Regex r = new Regex(@"hd.*?\.");
Console.WriteLine(r.Match(s).Value);
于 2013-01-03T19:49:26.133 回答
0

我在网上找到了一个有效的功能......

            public static string RemoveBetween(string s, string begin, string end)
            {
                Regex regex = new Regex(string.Format("{0}{1}", begin, end));
                return regex.Replace(s, string.Empty);
            }
于 2013-01-03T20:01:02.970 回答
0

也许你也可以使用 string.split:

List<string> list = yourString.Split(".");
List<string> keeping = list.Where(s => !s.Contains("hd") && !s.Contains("hr"))
return String.Join(".", keeping);
于 2013-01-03T19:50:07.537 回答
0

你可以使用正则表达式

string data = "lg-270-110.sh-300-110.hd-180-1.hr-155-61.ea-1403-62.cc-3007-110-110.ch-220-63.ca-3084-64-64";

//Match things that are preceded by a dot (.) (or the beginning of input)
// that begin with 'h' followed by a single letter, then dash, three digits, 
// a dash, at least one digit, followed by a period (or the end of input)
var rx = new Regex(@"(?<=(^|\.))h\w-\d{3}-\d+(\.|$)");

//Get the matching strings found
var matches = rx.Matches(data);

//Print out the matches, trimming off the dot at the end
foreach (Match match in matches)
{
    Console.WriteLine(match.Value.TrimEnd('.'));
}

//Get a new copy of the string with the matched sections removed
var result = rx.Replace(data, "").TrimEnd('.');
于 2013-01-03T19:50:21.703 回答
0

您可能需要结合使用IndexOfSubstring(或可能的正则表达式)。你真的需要理解和解释你的字符串是如何构造的,以便提取你想要的数据。

IndexOf搜索字符串。它返回字符串中第一次出现的字母。它还可以在字符串中找到子字符串。它通常用于循环结构。当什么都没有找到时,它返回负数。

子字符串提取字符串。它要求您指明起始索引和长度。然后它返回一个包含该范围内字符的全新字符串

        string data = "lg-270-110.sh-300-110.hd-180-1.hr-155-61.ea-1403-62.cc-3007-110-110.ch-220-63.ca-3084-64-64";

        int indexOf_hd = data.IndexOf("hd");
        int indexOf_fullstop = data.IndexOf(".", indexOf_hd);

        string extracteddata = data.Substring(indexOf_hd, indexOf_fullstop - indexOf_hd);

        // extracteddata = hd-180-1

另请查看IndexOf示例和子字符串示例

于 2013-01-03T19:46:13.623 回答