Parse multiple doubles from a String

后端 未结 4 1518
轮回少年
轮回少年 2020-12-20 01:57

I would like to know how to parse several double numbers from a string, but string can be mixed, for instance: String s = \"text 3.454 sometext5.567568more_text\"

4条回答
  •  青春惊慌失措
    2020-12-20 02:52

    1. Parse out the substring (surrounded by whitespace)

    2. Use String.ParseDouble() to get the numeric value

    Here's one example, using "split()" to parse (there are many alternatives):

    // http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html
    
    String phrase = "the music made   it   hard      to        concentrate";
    String delims = "[ ]+";
    String[] tokens = phrase.split(delims);
    

    Here's a second alternative:

    Java: how to parse double from regex

提交回复
热议问题