Like:
"Name: Daniel --- Phone Number: 3128623432 --- Age: 12 --- Occupation: Student"
How to get what is after "Age"? I would only like the numbers. (his age)
Like:
"Name: Daniel --- Phone Number: 3128623432 --- Age: 12 --- Occupation: Student"
How to get what is after "Age"? I would only like the numbers. (his age)
Can you try a full code with following concept:
string strAge;
string myString = "Name: Daniel --- Phone Number: 3128623432 --- Age: 12 --- Occupation: Student";
int posString = myString.IndexOf("Age: ");
if (posString >0)
{
strAge = myString.Substring(posString);
}
Robust way of doing is to get some Regular Expressions :) though...
Supposing that you have age in this format Age: value
string st = "Name: Daniel --- Phone Number: 3128623432 --- Age: 12 --- Occupation: Student";
//Following Expression finds a match for a number value followed by `Age:`
System.Text.RegularExpressions.Match mt = System.Text.RegularExpressions.Regex.Match(st, @"Age\: \d+");
int age=0; string ans = "";
if(mt.ToString().Length>0)
{
ans = mt.ToString().Split(' ')[1]);
age = Convert.ToInt32(ans);
MessageBox.Show("Age = " + age);
}
else
MessageBox.Show("No Value found for age");
MessgeBox show you, your string value (if found)..
Actually you have data, which could be easily represented as dictionary of type Dictionary<string, string>
:
var s = "Name: Daniel --- Phone Number: 3128623432 --- Age: 12 --- Occupation: Student";
var dictionary = s.Split(new string[] { "---" }, StringSplitOptions.None)
.Select(x => x.Split(':'))
.ToDictionary(x => x[0].Trim(), x => x[1].Trim());
Now you can get any value from your input string:
string occupation = dictionary["Occupation"];
int age = Int32.Parse(dictionary["Age"]);