c# substring indexof
Asked Answered
C

5

8

i have a string which looks like this - "FirstName||Sam LastName||Jones Address||123 Main ST ..." (100 more different values)

I want to find only Sam and Jones from the entire string.

so string firstname = originalstring.substring ... etc.

Does anyone know how I can do this?

ADDITION - I think i forgot to mention couple of things.

FirstName||Sam\r\n MiddleName||\r\n LastName||Jones\r\n ....

So now if i count the number of characters that wont help me, cause could need more items other than just firstname and lastname.

Cavanagh answered 3/5, 2011 at 21:45 Comment(4)
I'm not clear on how the original string is formatted. It appears there is one item of data per line on your string, is this correct? Does each string have multiple person records, or is there only one person record in each string?Gesticulatory
When I took a peek behind your editing, it appeared that you have multiple lines. But the formatting that StackOverflow is applying to your question (actually that HTML is applying to your question) is smashing those down onto one line. Thus my confusion.Gesticulatory
I hope my explanation has cleared out your question Charlie. And to everyone here, a big thanks for trying to help me.Cavanagh
Is there only one person in the entire string, and you need the first and last name? Or are there multiple people in the string, and you need to find more than one last name and first name?Gesticulatory
N
8

Use Regular expressions:

string myString = "FirstName||Sam LastName||Jones Address||123 Main ST...";
string pattern = @"FirstName\|\|(\w+) LastName\|\|(\w+) ";
Match m = Regex.Match(myString, pattern);
string firstName = m.Groups[1].Value
string lastName = m.Groups[2].Value;

See its Demo here.

Nobell answered 3/5, 2011 at 21:51 Comment(1)
I like how no-one else suggested regex's :) +1 for that.Siddur
E
5

I think this might work better than the .Split approach. If you had || between 'Sam' and 'LastName' then you'd certainly want to .Split. As it is, this might be better.

    string inStr = "FirstName||Sam LastName||Jones Address||123 Main ST ";
    int fStart = inStr.IndexOf("FirstName") + "FirstName".Length + "||".Length;
    int fEnd = inStr.IndexOf(" LastName");

    string FirstName = inStr.Substring(fStart, fEnd - fStart);
Epigeal answered 3/5, 2011 at 21:50 Comment(0)
E
1

I would split the string twice once on " " and then again on || to get the values of first and last name

string [] ary = s.Split(" ");
string [] key;
string firstname;
string lastname;
foreach (string val in ary ) {
    key = val.Split("||");
    if ( key[0] == "FirstName") {
         firstname = key[1];
}
if ( key[0] == "LastName") {
   lastname = key[1];
}
}
Eyelash answered 3/5, 2011 at 21:47 Comment(4)
Works until you get a name containing a space.Commorant
Splitting on ' ' won't work even in the example - check the street name (if those are required anyway)Scene
splitting on ' ' will fail when there are spaces, i.e, Address||123 Main STResponsible
if you assume that the order remains constant and that names don't have spaces in them then it should be fine.Eyelash
G
0
str = Str.Split("||")[1].split(" ")[0] //Sam
str = Str.Split("||")[2].split(" ")[0] // Jones
Gem answered 3/5, 2011 at 21:48 Comment(0)
P
0

something like this string firstname = originalstring.substring(indexof("FirstName") + 11, ((indexof("LastName) - indexof("FirstName") + 11 )

this way you get to the first letter after || and till the first letter before "lastname" the same goes for surname you just switch firstname with lastname and lastname with adress

edit: my fault... it's not substring.(indexOf... but it's

originalString.Substring(origianlString.IndexOf("FirstName) + 11, (originalString.IndexOf("LastName") - originalString.IndexOf("FirstName") + 11) and when looking for last name it's not + 11 but 10 because "LastName".Length + + "||".Length = 10

Persona answered 3/5, 2011 at 21:52 Comment(1)
Rob P. made it a bit cleaner then me:)Jadajadd

© 2022 - 2024 — McMap. All rights reserved.