Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to split a line given below..


First Class Mail (Canada) (15.00)


From the above line i want only 15.00


How do i use the string.Split();


I tried Many ways but i didn't get

Help Me

Thanks ,
Bajid
Posted

String.Split is a very basic function: I'd use a Regular Expression which can be a lot more "intelligent" when it comes to string manipulations.
C#
public static Regex regex = new Regex(
      "(?<=\\()(\\d+\\.\\d{2})(?=\\))",
    RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );
...
Match m = regex.Match("First Class Mail (Canada) (15.00)");
if (m.Success)
   {
   string value = m.Value;
   ...
   }



"Do a favor

UPS NEXT DAY AIR (10.00 + 17.44 = 27.44)


How can we get 27.44 from above Line."



That takes a bit more thinking about, if you want to capture the originals as well...
Try this:
C#
public static Regex regex = new Regex(
      "(?<=\\()(?:[\\d\\s\\.+]+=\\s*)?(\\d+\\.\\d{2})(?=\\))",
    RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );


Which should capture the numeric part of both...
 
Share this answer
 
v2
Comments
Richard C Bishop 14-Feb-14 15:33pm    
Your solution is much cleaner than mine. I have not created any REGEX on my own, but that is a good idea in this case. +5.
OriginalGriff 14-Feb-14 15:41pm    
They can be difficult to work out, which stops a lot of people going near them.
Get a copy of Expresso
http://www.ultrapico.com/Expresso.htm

It's free, and it examines and generates Regular expressions. I use it a lot, and I wish I'd written it!
Richard C Bishop 14-Feb-14 15:48pm    
Interesting, thank you.
Bajid Khan 14-Feb-14 16:08pm    
It's Superb ... Thanq Very Much..
OriginalGriff 14-Feb-14 16:24pm    
You're welcome!
You can use it like this:
string initialString = "First Class Mail (Canada) (15.00)";
string[] splitString = initialString.Split(' ');
string result = (splitString[4].ToString()).Replace('(','').Replace(')','');

This is just a basic example to give you an understanding.
 
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