Reputation: 159
I need to get 4 variable from a string
That's a resquest someone can make to a server :
String request= "Jouer_un_bulletin <nbT> <mise> <numeros_grilleA> <numeros_grilleB>"
I need to get nbt
, mise
, numeros_grilleA
and numeros_grilleB
Upvotes: 1
Views: 69
Reputation: 186668
You can try using regular expressions:
String request = "Jouer_un_bulletin <nbT> <mise> <numeros_grilleA> <numeros_grilleB>";
String pattern = @"(?<=<)[^<>]*(?=>)";
String[] prms = Regex
.Matches(request, pattern)
.OfType<Match>()
.Select(match => match.Value)
.ToArray();
Test:
// nbT
// mise
// numeros_grilleA
// numeros_grilleB
Console.Write(String.Join(Environment.NewLine, prms));
Upvotes: 4
Reputation: 3764
You need to parse the request. How to do this is entirely based on what you expect to receive and how you validate it. Assuming (and this is a big assumption) that you will always get the format above, you can use String.Split to split the string on the open bracket character, and then take the component pieces (ignoring the first one) and trim off the close bracket and any additional spaces. These will be your variables. This is not, by any means, a good way of sending data, and you should at a minimum validate this data a lot before using it.
The very basic (and by all means this is terrible code you shouldn't use) concept is:
String request= "Jouer_un_bulletin <nbT> <mise> <numeros_grilleA> <numeros_grilleB>";
var pieces = request.Split('<');
var strList = new List<string>();
for(int i = 1 ; i < pieces.Length; i++)
{
strList.Add(pieces[i].Trim(' ','>'));
}
Upvotes: 0