Parsing string

  • Thread starter Thread starter Netmonster
  • Start date Start date
N

Netmonster

Hello all,

I am creating a string based on data returned from an oracle db into a
hashtable.

string tmpFirstName = tmphashtable["FIRST_NAME"].ToString();

The Database has a column called FIRST_NAME but the return also has the
middle initial so my string look something like this "FirstName M" I
don't need the M. Can nay one show me an example on how to parse the
space and the M from the string to just get the FirstName?
Thanks,

KC
 
I suppose the other catch is someone who has two words as a first name, or
more than one initial.

something like?

// convert "Freg H" to "Fred"
if ( firstName[ firstName.Length - 2 ] == " " )
firstName = firstName.SubString ( 0, firstName.Length - 2 );

// convert "Freg JH" to "Fred"
else if firstName[ firstName.Length - 3 ] == " " )
firstName = firstName.SubString ( 0, firstName.Length - 3 );
 
KC,

First, if the column is a string, you can most likely just cast it to a
string. Calling ToString is superfluous:

string tmpFirstName = (string) tmphashtable["FIRST_NAME"];

Then, you can call the Split method on the string, and get an array of
the parts delimited by whatever you choose. In this case, it would be a
space.

// The parts.
string[] parts = tmpFirstName.Split(new char[]{' '});

The first name would be in the array at index 0 then.

Hope this helps.
 
Dan Bass said:
I suppose the other catch is someone who has two words as a first name, or
more than one initial.

I think a name like J Edgar [Hoover] would fail. I think the first thing you
should do is to scan the text for space characters. Only then should you try
to split the string. Don't. make the assumptions about the location of this
character.

/Fredrik
 
KC

Alternavily from splitting.

if (str.IndexOf(" ") != -1) str = str.Substring(0, str.IndexOf(" "));
I hope this helps,

Cor
 
Out of curiosity, why is it better to use a string cast rather than
ToString()? Is it a performance issue?

/Mats-Lennart

Nicholas Paldino said:
KC,

First, if the column is a string, you can most likely just cast it to a
string. Calling ToString is superfluous:

string tmpFirstName = (string) tmphashtable["FIRST_NAME"];

Then, you can call the Split method on the string, and get an array of
the parts delimited by whatever you choose. In this case, it would be a
space.

// The parts.
string[] parts = tmpFirstName.Split(new char[]{' '});

The first name would be in the array at index 0 then.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Netmonster said:
Hello all,

I am creating a string based on data returned from an oracle db into a
hashtable.

string tmpFirstName = tmphashtable["FIRST_NAME"].ToString();

The Database has a column called FIRST_NAME but the return also has the
middle initial so my string look something like this "FirstName M" I
don't need the M. Can nay one show me an example on how to parse the
space and the M from the string to just get the FirstName?
Thanks,

KC
 
Back
Top