2 Replies - 19872 Views - Last Post: 03 March 2008 - 04:32 PM Rate Topic: -----

#1 access2reality   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 10-February 08

How can i cut some of this string?

Posted 03 March 2008 - 03:47 PM

Hi,
I have a small problem . here is :
i have a string that i get from a list box the string comeing like this for exampel :

jack(i am at home)

now i want get just jack form this text . and also jack is different's anytime .

i use this code for separate jack from others:
			string name,message= listbox1.Text;
			string[] textcuter= message.Split(' ');
			name=text2[0];


but i get output like this :
jack(i am

how can i get just jack in this exampel?

thanks and i be waiting for your help

This post has been edited by access2reality: 03 March 2008 - 04:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How can i cut some of this string?

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: How can i cut some of this string?

Posted 03 March 2008 - 04:10 PM

If "(" is always there, you could split using that character. That or you could use a regular expression to pattern match the name. Either way will work. Here is a nice example of the regex solution...

// Match the string to pull off name before parenthesis 
// (letters, numbers, spaces are matched)
Regex exp = new Regex(@"^[a-zA-Z0-9\s]+");
Match aMatch = exp.Match("jack(i am home)");

if (aMatch != null) { Console.WriteLine(aMatch.ToString()); }



So what we are doing here is we are pulling off any letters, numbers, spaces off the front of the string. It will stop at the first non defined character (aka the parenthesis in this case).

Give it a whirl and be sure to include System.Text.RegularExpressions as one of your using statements. :)

"At DIC we be regular ninjas throwing around regular expressions!" :snap:

This post has been edited by Martyr2: 03 March 2008 - 04:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 access2reality   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 10-February 08

Re: How can i cut some of this string?

Posted 03 March 2008 - 04:32 PM

View PostMartyr2, on 3 Mar, 2008 - 04:10 PM, said:

If "(" is always there, you could split using that character. That or you could use a regular expression to pattern match the name. Either way will work. Here is a nice example of the regex solution...

// Match the string to pull off name before parenthesis 
// (letters, numbers, spaces are matched)
Regex exp = new Regex(@"^[a-zA-Z0-9\s]+");
Match aMatch = exp.Match("jack(i am home)");

if (aMatch != null) { Console.WriteLine(aMatch.ToString()); }



So what we are doing here is we are pulling off any letters, numbers, spaces off the front of the string. It will stop at the first non defined character (aka the parenthesis in this case).

Give it a whirl and be sure to include System.Text.RegularExpressions as one of your using statements. :)

"At DIC we be regular ninjas throwing around regular expressions!" :snap:


Hi,
wow it work !!! thanks alot man . i hop one day i help you .
thanks agine for teaching me this method.
good luck
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1