Welcome to Dream.In.Code
Become a C# Expert!

Join 150,396 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,024 people online right now. Registration is fast and FREE... Join Now!




How can i cut some of this string?

 
Reply to this topicStart new topic

How can i cut some of this string?

access2reality
3 Mar, 2008 - 02:47 PM
Post #1

New D.I.C Head
*

Joined: 10 Feb, 2008
Posts: 24


My Contributions
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:
CODE

            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: 3 Mar, 2008 - 03:20 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: How Can I Cut Some Of This String?
3 Mar, 2008 - 03:10 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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...

csharp

// 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. smile.gif

"At DIC we be regular ninjas throwing around regular expressions!" decap.gif

This post has been edited by Martyr2: 3 Mar, 2008 - 03:11 PM
User is offlineProfile CardPM
+Quote Post

access2reality
RE: How Can I Cut Some Of This String?
3 Mar, 2008 - 03:32 PM
Post #3

New D.I.C Head
*

Joined: 10 Feb, 2008
Posts: 24


My Contributions
QUOTE(Martyr2 @ 3 Mar, 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...

csharp

// 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. smile.gif

"At DIC we be regular ninjas throwing around regular expressions!" decap.gif


Hi,
wow it work !!! thanks alot man . i hop one day i help you .
thanks agine for teaching me this method.
good luck

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 06:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month