TimeTravelPenguin
TimeTravelPenguin

Reputation: 340

Splitting string for separate information

I have a textbox called txt_Input with text that looks as follows:

1st. User01 12"80
2nd. User02 12"83
3rd. User03 12"87
4th. User 04 13"03
5th. User0 5 13"10    
etc.

I know you can split a string using string.Split(value), however in this example, I am unsure as to how to do so. I wish to split The Username and time portions into their own variables name and time. The troubling issue is, however, the username can have spaces.

Removing the ordinal rank is simple by using str = str.Substring(5);, which removes everything up to the 1st character of the username (for ranks 0 to 9)

I will be adding these variables to an object in a list, so I am trying to do something to the likes of:

private void btn_Submit_Click(object sender, EventArgs e)
{
    List<Competitor> compList;
    for (int i = 0; i < txt_Input.Lines.Length; i++)
    {
        // Code to separate username and time into varaibles

        Competitor competitor = new Competitor(Username, Time);
        compList.Add(competitor)
    }
}

Upvotes: 0

Views: 48

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521399

You may try splitting each row using a lookahead:

string input = "User 04 13\"03";
var items = Regex.Split(input, @"(?=\d{2}""\d{2}$)");
foreach (string item in items)
{
    Console.WriteLine(item);
}

User 04 
13"03

Upvotes: 1

Related Questions