Jump to content
  • Advertisement
Sign in to follow this  

Parsing a string in C#

This topic is 4347 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to parse a string so that when the user types "Lastname, Firstname" it catches the lastname into one string and the first name into another string - and allows for the user to use the backspace and work properly. the only thing i could think of is check every keystroke for ',' but how would I save the 1st string to a temp string, and then save only the 2nd string to a temp string?

Share this post


Link to post
Share on other sites
Advertisement
It sounds like you're working with a textbox here. Wait until the user has type in the complete string, then parse it. Its properly more trouble than its worth doing it on the fly. Anyway, when you have the complete string, search for , and make substrings: Look up the IndexOf and Substring methods of the String class.

If this doesn't help, you must provide a little more info about the problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by MCeltic33
I need to parse a string so that when the user types "Lastname, Firstname" it catches the lastname into one string and the first name into another string - and allows for the user to use the backspace and work properly.

the only thing i could think of is check every keystroke for ',' but how would I save the 1st string to a temp string, and then save only the 2nd string to a temp string?

Wait for the TextBox to lose focus (check the LostFocus event), then check. Parse by using String.Split on the comma; if there's more than one comma, you can raise a validation error. The previous poster's suggestions of using IndexOf() and Substring() are a little too clunky for this particular case.

hope that helps,

Share this post


Link to post
Share on other sites
Quote:
Original post by Hesterumpe
It sounds like you're working with a textbox here. Wait until the user has type in the complete string, then parse it. Its properly more trouble than its worth doing it on the fly. Anyway, when you have the complete string, search for , and make substrings: Look up the IndexOf and Substring methods of the String class.

If this doesn't help, you must provide a little more info about the problem.


The problem with this is I need to update the display as the user is typing...

Share this post


Link to post
Share on other sites
derive a control from textbox, leave the standard Text property functioning as is, but add an additional property that is an array of strings, something like Tokens. Every change, reparse the string and set the Tokens property accordingly, and fire appropriate events.

Share this post


Link to post
Share on other sites
To find out in "real time" what the user is typing, you just need to handle the "KeyPress" event. Add an event handler to handle this event, and this will get called every time the user has pressed a key. Then, when the user has typed something, you can get the tokens using one of the techniques described in this thread.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!