开发者

C# string manipulation

I have a string like A150[ff;1];A160开发者_JAVA百科;A100;D10;B10'

in which I want to extract A150, D10, B10

In between these valid string, i can have any characters. The one part that is consistent is the semicolumn between each legitimate strings.

Again the junk character that I am trying to remove itself can contain the semi column


Without having more detail for the specific rules, it looks like you want to use String.Split(';') and then construct a regex to parse out the string you really need foreach string in your newly created collection. Since you said that the semi colon can appear in the "junk" it's irrelevant since it won't match your regex.


        var input = "A150[ff+1];A160;A150[ff-1]";
        var temp = new List<string>();
        foreach (var s in input.Split(';'))
        {
            temp.Add(Regex.Replace(s, "(A[0-9]*)\\[*.*", "$1"));
        }
        foreach (var s1 in temp.Distinct())
        {
            Console.WriteLine(s1);   
        }

produces the output

A150
A160


First,you should use string s="A150[ff;1];A160;A100;D10;B1"; s.IndexOf("A160"); Through this command you can get the index of A160 and other words. And then s.Remove(index,count).


If you only want to remove the 'junk' inbetween the '[' and ']' characters you can use regex for that

   Regex regex = new Regex(@"\[([^\}]+)\]");

   string result = regex.Replace("A150[ff;1];A160;A100;D10;B10", "");

Then String.Split to get the individual items

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜