# ASP.NET/C#: Parse String, Get Three Numbers

Using ASP.NET/C#, I would like to parse this:
Round 1 (\$12,34|\$56.78|\$98.76)

So that:
double a = 12.34;
double b = 56.78;
double c = 98.76;

But if the string is like this:
Round 1 (\$12,34|\$98.76)
double a = 12.34;
double b = 0;
double c = 98.76;
LVL 10
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Senior DeveloperCommented:
Try the following code:

``````        private void button1_Click(object sender, EventArgs e)
{
string strToParse = @"Round 1 (\$12.34|\$56.78|\$98.76)";
double a;
double b;
double c;

int index1 = strToParse.IndexOf("(");
int index2 = strToParse.IndexOf(")");
string temp = strToParse.Substring(index1+1, index2 - index1-1);
temp = temp.Replace("\$", "");
char[] separator = {'|'};
string[] numberStrs = temp.Split(separator);

if (numberStrs.Length == 3)
{
a = Double.Parse(numberStrs[0]);
b = Double.Parse(numberStrs[1]);
c = Double.Parse(numberStrs[2]);
}
else
{
a = Double.Parse(numberStrs[0]);
b = 0;
c = Double.Parse(numberStrs[1]);
}

System.Diagnostics.Debug.WriteLine("a = " + a + "; b = " + b + "; c = " + c);
}
``````

The code above has no any error handling or checking for proper number format.
You may need to consider cases when numbers are not in a correct format (or not numbers), when number of entries not 2 or 3, etc.

Output:
a = 12.34; b = 56.78; c = 98.76
or
a = 12.34; b = 0; c = 98.76
Business Systems Analyst , ex-Senior Application EngineerCommented:
you can also try use regular expression to do this:

``````using System.Text.RegularExpressions;
private Boolean rtn(String s, String re) {
var e = new Regex(re);
if (e.IsMatch(s)) {
return true;
} else {
return false;
}
}
String p1 = @"Round 1 \(\\$(?<v1>[^|]+)\|\\$(?<v2>[^|]+)\|\\$(?<v3>[^|]+)\)";
String p2 = @"Round 1 \(\\$(?<v1>[^|]+)\|\\$(?<v3>[^|]+)\)";
var e1 = new Regex(p1);
var e2 = new Regex(p2);
String t = @"Round 1 (\$12,34|\$56.78|\$98.76)";
if (rtn(t, p1)) {
var m1 = e1.Match(t);
Console.WriteLine("v1 = {0}, v2 = {1}, v3 = {2}", m1.Groups["v1"], m1.Groups["v2"], m1.Groups["v3"]);
} else {
var m2 = e2.Match(t);
Console.WriteLine("v1 = {0}, v2 = {1}, v3 = {2}", m2.Groups["v1"], "0", m2.Groups["v3"]);
}
t = @"Round 1 (\$12,34|\$98.76)";
if (rtn(t, p1)) {
var m1 = e1.Match(t);
Console.WriteLine("v1 = {0}, v2 = {1}, v3 = {2}", m1.Groups["v1"], m1.Groups["v2"], m1.Groups["v3"]);
} else {
var m2 = e2.Match(t);
Console.WriteLine("v1 = {0}, v2 = {1}, v3 = {2}", m2.Groups["v1"], "0", m2.Groups["v3"]);
}
``````

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
** No Points **

If I were a betting man, then I'd say you're probably not going to be dealing with "Round 1" every single time. If such be the case, then you can abstract the 1 away be turning it into a digit search (\d+) in ryancys' suggestion:

``````String p1 = @"Round \d+ \(\\$(?<v1>[^|]+)\|\\$(?<v2>[^|]+)\|\\$(?<v3>[^|]+)\)";
``````
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.