Click here to Skip to main content
15,905,915 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I need to extract the 33345.002 from the following string:

"ABC(MAX)(33345.002)"

How can I perform this in c#?
I tried handling it in SQL but was picking up the (MAX) too so now I'm gonna try c#.
Thanks
Posted
Comments
hzawary 3-Nov-11 21:27pm    
Solved the issue?
Mastersev 3-Nov-11 21:31pm    
nope :(
hzawary 3-Nov-11 21:39pm    
Did you try my solution!
Mastersev 3-Nov-11 21:41pm    
string value = "White BOX, Inc. (037913.00)";
string[] parts = value.Split(new char[] {'(', ')'}, System.StringSplitOptions.RemoveEmptyEntries);
string numeric = parts[parts.Length-1]; //sic!
double d = double.Parse(numeric);

This returns a double ---> 37913
I need --> 037913.00
hzawary 3-Nov-11 21:46pm    
While converted to double left zeros to be removed, so you must save your number in string before to convert it!

I would go for a regular expression.

You can find some informations here.

And there is a tool, Expresso, most useful to build your expressions and test them against as much entries as you want/need to.

Having a look at regular expressions is worth the additional learning time ; you will find they can be handy in many situations.

For your question, it would be (updated for variable lengths and any alphanumeric character) :

^(?:[\w]+\([\w]+\))\((?<NamedValue>[\d]+(?:\.[\d]+)?)\)$

Which describes the chain as :
Begin of line - One or more letters, followed by one or more letters enclosed in parenthesis, followed by a named capture group enclosed in parenthesis. This named capture group is composed of one or more digits, followed or not by a point and one or more digits - End of line

You can obtain the string representation of your double in NamedValue.

You can convert it to double by using the method hzawary told you, but passing only the NamedValue you got. It can be great to use Double.TryParse() to handle problematic entries.

double d;
bool ok = double.TryParse(yourMatch.Group["NamedValue"].Value, out d);


Hope this helps.
 
Share this answer
 
v6
Comments
phil.o 3-Nov-11 22:33pm    
Thank you for the 1 vote Oo
Mastersev 4-Nov-11 15:31pm    
I'll vote 5 because you helped me.
Here is the corrected solution based on Split:

C#
static double ExtractFrom(string value) {
   string[] parts = value.Split(new char[] {'(', ')'}, System.StringSplitOptions.RemoveEmptyEntries);
   string numeric = parts[parts.Length-1]; //sic!
   return double.Parse(numeric);
}


See also the Regex-based solution by Phil. It can be correct, I did not test it.

—SA
 
Share this answer
 
v2
Comments
hzawary 3-Nov-11 21:12pm    
Dear SAKryukov, May you see... 'Why "this.DragMove()" hold up some of other "events"? Not Solved Yet!' http://www.codeproject.com/Questions/277094/Why-DragMove-hold-up-some-of-other-event
Sergey Alexandrovich Kryukov 3-Nov-11 21:21pm    
Yes, I know the problem, thanks for reminding it to me. I did not try yet, but I recently tried DragMove -- it works. Mark Salsbery correctly mentioned that this API blocks some events (actually just consumes them), that's the problem. You should explain what else do you want to achieve as DragMove just works without isMouseDown. So, do you need anything else?
--SA
Mastersev 3-Nov-11 21:20pm    
It just grabs the 37913 from "White BOX, Inc. (037913.00)"
Sergey Alexandrovich Kryukov 3-Nov-11 21:24pm    
No! You did something wrong. It grabs the last term in brackets, which is 33345.002. Do you want me to test it? Did you add a separator '.', by any chance? Did you Parse by the method of double?
--SA
Sergey Alexandrovich Kryukov 3-Nov-11 21:31pm    
No! I tried not to believe my eyes (who knows?) so I just tested it -- it works correctly.
Now, you try.

When you are convince yourself, please accept the solution formally (green button) -- thanks.
--SA
Try this it also uses a regex:

C#
string temp = "YYY(33345.002)(gg)YYYY";
temp = Regex.Replace(temp, "[^.0-9]", "");
double num;
bool success = Double.TryParse(temp, out num);

if (success)
{
    //do what ever to the number
}
 
Share this answer
 
Comments
Mastersev 3-Nov-11 21:11pm    
Thanks Ninja your answer is the best answer so far. But there is something new I forgot to mention. Some of the numbers have zeros before them. i.e. 00333.33

They are IDs :(
phil.o 3-Nov-11 21:11pm    
There would be a problem if there were to be a point anywhere else than in the numeric part.
Mastersev 3-Nov-11 21:14pm    
Yea in case of "White BOX, Inc. (037913.00)" it does not work.
C#
double x = Convert.ToDouble("ABC(MAX)(33345.002)".Substring(9, 9));


Try this one and just you input the characters to remove!
C#
double x = Convert.ToDouble("ABC(MAX)(33345.002)".ToUpper().Trim(new char[] { 'A', 'B', 'C', 'M', 'X', '(', ')' }));
 
Share this answer
 
v3
Comments
Mastersev 3-Nov-11 20:41pm    
Sorry for the confusion but it may be "BADLands(O)(33345.002)". No fix length.
hzawary 3-Nov-11 20:47pm    
is fixed "ABC(MAX)("?
Mastersev 3-Nov-11 20:48pm    
no can be anything
hzawary 3-Nov-11 20:55pm    
Ok! so you want double only!?
Mastersev 3-Nov-11 20:55pm    
Double and int
C#
string[] MyString = Strings.Split("ABC(MAX)(33345.002)", "(");
MyString(2) = MyString(2).Remove(MyString(2).IndexOf(")"));
//Then call MyString(2)
 
Share this answer
 
v3
Comments
Mastersev 3-Nov-11 20:49pm    
What if it's "YYY(33345.002)(gg)YYYY"?
WhiteKnightBRASIL 3-Nov-11 20:52pm    
There, it's updated ;)
WhiteKnightBRASIL 3-Nov-11 20:54pm    
But you need to fixate where the data is. So in THAT example ("YYY(33345.002)(gg)YYYY") you need to use MyString(1) instead of MyString(2)
Mastersev 3-Nov-11 20:56pm    
I have 130000 records I cannot check them one by one
WhiteKnightBRASIL 3-Nov-11 21:04pm    
Will the numbers you need ALWAYS have a "(" before them?

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