如何在java中的特定字符后修剪字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18220022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trim a string after a specific character in java
提问by user2430771
I have a string variable in java having value:
我在java中有一个字符串变量具有值:
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.
我怎样才能做到这一点?我是java编程语言的新手。
Thanks,
谢谢,
采纳答案by alfasin
You can use:
您可以使用:
result = result.split("\n")[0];
回答by arshajii
Assuming you just want everything before \n
(or any other literal string/char), you should use indexOf()
with substring()
:
假设您只想要之前的所有内容\n
(或任何其他文字字符串/字符),您应该使用indexOf()
with substring()
:
result = result.substring(0, result.indexOf('\n'));
If you want to extract the portion before a certain regular expression, you can use split()
:
如果要提取某个正则表达式之前的部分,可以使用split()
:
result = result.split(regex, 2)[0];
String result = "34.1 -118.33\n<!--ABCDEFG-->";
System.out.println(result.substring(0, result.indexOf('\n')));
System.out.println(result.split("\n", 2)[0]);
34.1 -118.33 34.1 -118.33
(Obviously \n
isn't a meaningful regular expression, I just used it to demonstrate that the second approach also works.)
(显然\n
不是一个有意义的正则表达式,我只是用它来证明第二种方法也有效。)
回答by óscar López
Try this:
尝试这个:
String result = "34.1 -118.33\n<!--ABCDEFG-->";
result = result.substring(0, result.indexOf("\n"));
回答by gparyani
Use a Scanner
and pass in the delimiter and the original string:
使用 aScanner
并传入分隔符和原始字符串:
result = new Scanner(result).useDelimiter("\n").next();
回答by Pshemo
How about
怎么样
Scanner scanner = new Scanner(result);
String line = scanner.nextLine();//will contain 34.1 -118.33
回答by Bohemian
Use regex:
使用正则表达式:
result = result.replaceAll("\n.*", "");
replaceAll()
uses regex to find its target, which I have replaced with "nothing" - effectively deleting the target.
replaceAll()
使用正则表达式查找其目标,我已将其替换为“无” - 有效地删除了目标。
The target I've specified by the regex \n.*
means "the newline char and everything after"
我由正则表达式指定的目标\n.*
意味着“换行符和之后的所有内容”
回答by Abstract
You could use result = result.replaceAll("\n","");
or
你可以使用 result = result.replaceAll("\n","");
或
String[] split = result.split("\n");
回答by samlewis
There are many good answers, but I would use StringUtils
from commons-lang. I find StringUtils.substringBefore()
more readable than the alternatives:
有很多很好的答案,但我会使用StringUtils
来自commons-lang。我发现StringUtils.substringBefore()
比替代方案更具可读性:
String result = StringUtils.substringBefore("34.1 -118.33\n<!--ABCDEFG-->", "\n");
回答by irshad
you can use StringTokenizer:
你可以使用StringTokenizer:
StringTokenizer st = new StringTokenizer("34.1 -118.33\n<!--ABCDEFG-->", "\n");
System.out.println(st.nextToken());
output:
输出:
34.1 -118.33