Jitesh Dani
Jitesh Dani

Reputation: 385

C# string manipulation

I have a string like A150[ff;1];A160;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

Upvotes: 0

Views: 354

Answers (4)

Matt
Matt

Reputation: 3014

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

Upvotes: 0

Phillip Ngan
Phillip Ngan

Reputation: 16106

        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

Upvotes: 1

nithins
nithins

Reputation: 3192

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.

Upvotes: 3

user189594
user189594

Reputation: 191

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).

Upvotes: 0

Related Questions