What is the best way to parse the string in C#

In java u have a class like Scanner. Is there any equivalent class in c#.

Recommended Answers

All 2 Replies

You can use the Split() method of the string to divide it up by a set of delimiters into a string array. Then you can use the static TryParse method in any of the numeric types (e.g., Int32.TryParse(), float.TryParse(),and for double, Int16,Int64, even boolean) to get numbers from the individual strings. The TryParse methods are a bit like those in the Scanner class (it's been a while since I've worked with Java but with TryParse I don't think (AFAIK) you can use it to go from one number in a string to the next, for example).

Note: TryParse returns a bool value as to whether or not it converted the number. If you are into exceptions you can use the Parse method of each of those (e.g., Int32.Parse()) but it will throw an exception when it cannot convert. See the documentation below for some examples.

String.Split()

Int32.TryParse()
(others will be similar)

Post back with any further issues or if I misunderstood what you wanted.

You can use the Split() method of the string to divide it up by a set of delimiters into a string array. Then you can use the static TryParse method in any of the numeric types (e.g., Int32.TryParse(), float.TryParse(),and for double, Int16,Int64, even boolean) to get numbers from the individual strings. The TryParse methods are a bit like those in the Scanner class (it's been a while since I've worked with Java but with TryParse I don't think (AFAIK) you can use it to go from one number in a string to the next, for example).

Note: TryParse returns a bool value as to whether or not it converted the number. If you are into exceptions you can use the Parse method of each of those (e.g., Int32.Parse()) but it will throw an exception when it cannot convert. See the documentation below for some examples.

String.Split()

Int32.TryParse()
(others will be similar)

Post back with any further issues or if I misunderstood what you wanted.

Thanks for reply...
But I have found this amazing class called Regex in C#,
where I can write my own grammar and without parsing that string do any operation on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.