How to trim a string after a specific character in

2020-05-21 08:02发布

I have a string variable in java having value:

String result="34.1 -118.33\n<!--ABCDEFG-->";

I want my final string to contain the value:

String result="34.1 -118.33";

How can I do this? I'm new to java programming language.

Thanks,

标签: java
9条回答
叛逆
2楼-- · 2020-05-21 08:54

you can use StringTokenizer:

StringTokenizer st = new StringTokenizer("34.1 -118.33\n<!--ABCDEFG-->", "\n");
System.out.println(st.nextToken());

output:

34.1 -118.33
查看更多
霸刀☆藐视天下
3楼-- · 2020-05-21 08:55

You can use:

result = result.split("\n")[0];
查看更多
ら.Afraid
4楼-- · 2020-05-21 08:56

Use a Scanner and pass in the delimiter and the original string:

result = new Scanner(result).useDelimiter("\n").next();
查看更多
登录 后发表回答