473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Parsing string

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

Nov 16 '05 #1
6 3310
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 );


"Netmonster" <ne***********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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

Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Netmonster" <ne***********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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

Nov 16 '05 #3
Thanks Nicholas.. Splitting the string worked great..

KC

Nov 16 '05 #4

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:eu****************@tk2msftngp13.phx.gbl...
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
Nov 16 '05 #5
KC

Alternavily from splitting.

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

Cor
Nov 16 '05 #6
Out of curiosity, why is it better to use a string cast rather than
ToString()? Is it a performance issue?

/Mats-Lennart

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eC**************@TK2MSFTNGP11.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Netmonster" <ne***********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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


Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
6
by: BerkshireGuy | last post by:
Does anyone know of a good function that will parse out parts of an SQL statement that is passed to it in seperate variables? It should be able to parse statements that contain ORDERBY, WHERE,...
9
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an...
3
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception:...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
2
by: RG | last post by:
I am having trouble parsing the data I need from a Serial Port Buffer. I am sending info to a microcontroller that is being echoed back that I need to remove before I start the actual important...
6
by: gw7rib | last post by:
I have a program that needs to do a small amount of relatively simple parsing. The routines I've written work fine, but the code using them is a bit long-winded. I therefore had the idea of...
1
by: hd95 | last post by:
In a perfect world my xml feed source would produce perfect xml ..that is not the case I am parsing an XML feed that sometimes has ampersands and dashes in the content that messes up my parsing. ...
1
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.