简体   繁体   中英

Extract the value of double quotation or String of string variable

I want to extract the value of double quotation or String of string variable for example. ""14.62647""

I want to get the value 14.62647 .

Solution1

If the string can contain multiple results you can use this regex \\"\\"(.*?)\\"\\" with Pattern to get any thing between double quotation like this :

String str = "some string \"\"14.62647\"\" some string";
String regex = "\"\"(.*?)\"\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);

while (matcher.find()) {                                                
    System.out.println(matcher.group(1));
}

Output

14.62647

regex demo


Solution2

if the string contain only one result you can use String::replaceAll to replace every thing with the matched group between double quotation :

String str = "\"\"14.62647\"\"";
str = str.replaceAll("\"\"(.*?)\"\"", "$1");//output = 14.62647

use a String#replace method:

    String myDoubleString = "\"\"14.62647\"\"";
    String result = myDoubleString.replace("\"\"", "");
    System.out.println(result);

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