operations on string

M

mamin

Hi all,
Anyone know some smart way to get two first substrings from string, in
example how to get string
"aaaa bbbb" from string "aaaa bbbb cccc ddddddd"?
 
A

Ananth Ramasamy Meenachi

Hi,

Please find the method below which will return string;

private string GetSecondSubstring(string str)
{
string[] Str;
char dlimit = ' ';

if (str.Length !=0)
{
Str = str.Split(dlimit);

if (Str.GetUpperBound(0) > 1)
str = Str[0] + dlimit + Str[1];

}
return str;
}
 
M

marss

(e-mail address removed) напиÑав:
Hi all,
Anyone know some smart way to get two first substrings from string, in
example how to get string
"aaaa bbbb" from string "aaaa bbbb cccc ddddddd"?


string str = "aaaa bbbb cccc ddddddd";
System.Text.RegularExpressions.Regex re = new
System.Text.RegularExpressions.Regex(@"^\w+\s+\w+\b");
System.Text.RegularExpressions.Match match = re.Match(str);
if (match.Success)
{
string res = match.Value;
//...........
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,091
Messages
2,570,604
Members
47,224
Latest member
Gwen068088

Latest Threads

Top