简体   繁体   中英

How do I extract multiple numbers from a string?

I want to extract three doubles from this string. The is a space between the numbers but nothing else.

string A = "3.1415 2.71828 1729.0"

Use the Split() method.

string[] nums = A.Split(' ');

Then do the conversion.

Updated: Changed double quotes to single quotes

You can do this by using the String.Split() method combined with LINQ:

string A = "3.1415 2.71828 1729.0";
double[] numbers = A.Split().Select(x=>double.Parse(x.Replace(".",","))).ToArray();

In the above example the decimal numbers in string A are converted from string to double and stored in a double array, but if you want to store your decimal numbers as separate strings in a string array then use:

string[] numbers = A.Split();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM