Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone,
I hope someone can help me with this issue.

I have a list of strings as shown below:

12|string|string2|username_one|string3|somenumbers123456789012
123|string1|string2|username_two|string3|somenumbers34566789901234
1245|string1|string2|username_three|string3|somenumbers123456788900


What I need is to extract username from string, without "|" characters

Note that all strings and usernames are different lengths.

Thank you in advance!

Regards,
Aleksandar
Posted
Updated 16-Apr-15 10:09am
v2
Comments
Kornfeld Eliyahu Peter 16-Apr-15 14:05pm    
Please show what do you expect after the process...
jk0391 16-Apr-15 14:11pm    
Did you read the question?

"What I need is to extract username from string, without "|" characters"

Clearly there.

C#
string[] data = yourstring.Split('|');


data[3] will contain your username :)

Update: Because I saw some others try to solve fully...

Use this to extract data initially:

C#
string[] data = File.ReadAllLines("file were converting to string");
string[] usernames = new string[# of usernames];

for (i = 0; i < data.Count(); i++)
{
        string[] username = data[i].Split('|')[3];
}
 
Share this answer
 
v3
Comments
AlexaRS 17-Apr-15 2:39am    
Just what I needed!

Thank you Joe,
thank all of you, guys!
You can use Linq:

C#
List<string> myList = new List<string>(){@"12|string|string2|username_one|string3|somenumbers123456789012",
@"123|string1|string2|username_two|string3|somenumbers34566789901234",
@"1245|string1|string2|username_three|string3|somenumbers123456788900"};

//var qry = myList.Select(a=>Tuple.Create(a.Split('|')[0], a.Split('|')[1], a.Split('|')[2], a.Split('|')[3], a.Split('|')[4], a.Split('|')[5]));

var qry = myList.Select(a=>a.Split('|')).Select(a=>Tuple.Create(a[0], a[1], a[2], a[3], a[4], a[5]));
//for direct casting
//var qry = myList.Select(a=>a.Split('|')).Select(a=>new Tuple<string, string, string, string, string, string>(a[0], a[1], a[2], a[3], a[4], a[5]));
foreach(var row in qry)
{
    Console.WriteLine("{0} {1} {2} {3} {4} {5}", row.Item1, row.Item2, row.Item3, row.Item4, row.Item5, row.Item6 );
}


Result:
12   string  string2 username_one   string3 somenumbers123456789012 
123  string1 string2 username_two   string3 somenumbers34566789901234 
1245 string1 string2 username_three string3 somenumbers123456788900 


So, if you would like to get only user names, use:
C#
var qry = myList.Select(a=>a.Split('|')[3]);
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 16-Apr-15 16:45pm    
My 4. I would avoid hard-coding those strings, especially repeated '|'. (The inquirer may thing this is acceptable. :-)

At the same time, first list of strings is acceptable, as it can be considered as a mere test sample. I would also calculate the format string instead of hard-coding it, then it would work with any "params" array, not just with arrays of 6 elements.

Handling exception on the input data in invalid format? Something for the inquirer to think about, depending on the ultimate goals...

—SA
Maciej Los 16-Apr-15 17:00pm    
Agree. The difference between mine and other solutions is that that i use Tuple. I haven't enough time to think about method to create Tuple dunamically. You're right, Sergey - as always.
Thank you.
Sergey Alexandrovich Kryukov 16-Apr-15 18:10pm    
I understand, but I meant just the format string. But now I see another problem: you repeat split 6 times, this is wrong. You could split just once. And then, instead of Tuple, you could just use that array...
—SA
Maciej Los 17-Apr-15 1:39am    
OK, the answer has been updated ;)
You can see that the length of the strings is irrelevant...What is relevant that every string has the same number of parts, separated by the pipe sign (|)...
What you have to do is take every line and split it to its parts...
C#
string [] array = line.Split(new Char [] {'|'});

Now that array of string created has the exact same number of element and at the 3rd position you have the user-name...
C#
string user_name = array[3];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900