Alison
Alison

Reputation: 23

How can I parse a string into different strings in C#?

I just started work on a C# application. I need to break down a string into different parts. Is there an easy way to do this using C# patterns? I think I can do it with substrings but it might get messy and I want to do something that's easy to understand. Here's an example of the input:

AB-CDE-GHI-123-45-67-7777

variable1 = "AB-CDE-GHI"
variable2 = "123"
variable3 = "45"
variable4 = "67"
variable5 = "67-7777"

AB-CDE-GHIJKLM-123-45-67-7777

variable1 = "AB-CDE-GHIJKLM"
variable2 = "123"
variable3 = "45"
variable4 = "67"
variable5 = "67-7777"

AB-123-45-67-7777

variable1 = "AB"
variable2 = "123"
variable3 = "45"
variable4 = "67"
variable5 = "67-7777"

The first part of the string up until "123-45-67-7777" can be any length. Lucky for me the last part 123-45-67-7777 is always the same length and contains numbers that are zero padded.

I hope someone can come up with some suggestions for an easy method that uses regular expressions or something.

Input lines look like this:

aa-123-45-67-7777
HJHJH-123-45-67-7777
H-H-H--123-45-67-7777
222-123-45-67-7777

Upvotes: 1

Views: 134

Answers (5)

Akrem
Akrem

Reputation: 4652

like Oded say you can use string.Split

I edit my answer like you want

        string[] tab = textBox1.Text.Split('-');
        int length = tab.Length;
        string var1 = string.Empty;
        for(int i=0; i <= length-5 ; i++)
        {
           var1 = var1 + tab[i] + '-';
        }
        var1 = var1.Remove(var1.Length-1,1);
        string var2 = tab[length-4];
        string var3 = tab[length-3];
        string var4 = tab[length-2];
        string var5 = tab[length-2] + '-' + tab[length-1];

it's the same with the answer of @Govind KamalaPrakash Malviya just you have make var1 + tab[i]

Upvotes: 0

Oded
Oded

Reputation: 498942

You do not need RegEx for parsing this kind of input.

You can use string.Split, in particular if the input is highly structured.

If you first split by - you will get a string[] with each part in a different index of the array.

The length property of the array will tell you how many parts you got and you can use that to reconstruct the parts you need.

You can rejoin any of the bit you need back.

string[] parts = "AB-CDE-GHI-123-45-67-7777".split('-');

// joining together the first 3 items:
string letters = string.Format("{0}-{1}-{2}", parts[0], parts[1], parts[2]);

// letters = "AB-CDE-GHI"

If the number of sections is variable (apart from the last 4), you can use the length in a loop to rebuild the wanted parts:

StringBuilder sb = new StringBuilder();
for(int i = 0; i < parts.Length - 4; i++)
{
  sb.FormatAppend("{0}-", parts[i]);
}
sb.Length = sb.Length - 1; // remove trailing -

Upvotes: 1

Govind Malviya
Govind Malviya

Reputation: 13743

string[] str = "AB-CDE-GHI-123-45-67-7777".Split('-')

int a = str.Length;

variable1="";

for(int i=0;i=<a-5;i++)
{
variable1=str[i]+"-"+variable1;
}
// last - remove
variable1 = variable1.Remove(variable1.Length-1,1);

variable2 = str[a-4]
variable3 = str[a-3]
variable4 = str[a-2]
variable5 = str[a-2]+"-"str[a-1];

Upvotes: 0

Mike Goatly
Mike Goatly

Reputation: 7518

If the last part is always a known length (14 characters) you could just do something like this:

var firstPart = inputLine.Substring(inputLine.Length - 14);
var secondPart = inputLine.Substring(0, inputLine.Length - 15); // 15 to exclude the last -

Then you can just do your string splitting and job done :)

Upvotes: 1

Alon Gubkin
Alon Gubkin

Reputation: 57119

Although it is possible to use here String.Split, a better solution, in my opinion, would be to tokenize the input and then parse it.

You can use tools such as ANTLR for this purpose.

Upvotes: 0

Related Questions