Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

Please Tell me How to resolve this problem ...'

I have some string say

C#
string str = "ELSPending(250)";
string str2 = "Work In Progress(10)";


i want to take 250 and 10 from it please help me!

Thanks in advance

Shreeniwas Kushwah
Posted
Updated 16-Jul-13 19:56pm
v3
Comments
praks_1 17-Jul-13 1:44am    
Use split function of string
StM0n 17-Jul-13 1:50am    
There are so many ways to solve this... any specific requirements?

Hi,
Try this:
str.Split(new string[]{"(",")"},StringSplitOptions.RemoveEmptyEntries);
 
Share this answer
 
Comments
shreeniwas kushwah 17-Jul-13 1:47am    
ok
Easiest way is to use a regex:
(?:\()\d+(?:\))
should do it.
 
Share this answer
 
Comments
StM0n 17-Jul-13 1:57am    
Damn... I really wanted to avoid these this time :)
OriginalGriff 17-Jul-13 2:07am    
You could do it with IndexOf and Substring, but that's pretty much what the regex is doing anyway, with the added check for numeric only between the brackets.

Sometimes, a regex really is the simplest solution! :laugh:
StM0n 17-Jul-13 2:10am    
OP didn't ask for the simplest solution, but I also would use regexes ;)
My two cents with lambda :)
C#
string str  = "ELSPending(250)";
string str2 = "Work In Progress(10)";

Func<String, String> getContentBetweenBracketsFrom = Input => {
    return Input.Split(new char[] { '(', ')' }).ElementAt(1);
};

Console.WriteLine(getContentBetweenBracketsFrom(str));
Console.WriteLine(getContentBetweenBracketsFrom(str2));
Really a ton of possible solutions...
 
Share this answer
 
Hi sheeniwas,
Try this, it might help you.

C#
string str = "Work In Progress(10)";
int pos1 = str.IndexOf("(");
int pos2 = str.IndexOf(")");
int lenth = pos2 - pos1;
string newstr = str.Substring(pos1+1, lenth-1);


newstr will give you 10 in this case
If it is helpfull please mark it as answer.

Regards
SUNIL
 
Share this answer
 
v3
best way is to use regular expressions.
C#
string str="ELSPending(250)";
Regex regex = new Regex(".*\((.*\))");
Match matching=regex.Match(str);
string value;
if(matching.Success)
{
 value=matching.Groups[1].Value;
}
 
Share this answer
 
Hi, try to use this:

C#
string str = ELSPending(250);
string strOutput = str.Split(new char[] { '(', ')' })[1];

string str2 = Work In Progress(10);
string str2Output = str2.Split(new char[] { '(', ')' })[1];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900