简体   繁体   English

如何在java中的特定字符后修剪字符串

[英]How to trim a string after a specific character in java

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,谢谢,

您可以使用:

result = result.split("\n")[0];

Assuming you just want everything before \\n (or any other literal string/char), you should use indexOf() with substring() :假设您只想要\\n (或任何其他文字字符串/字符)之前的所有内容,您应该将indexOf()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不是一个有意义的正则表达式,我只是用它来证明第二种方法也有效。)

There are many good answers, but I would use StringUtils from commons-lang .有很多很好的答案,但我会使用commons-lang 中的StringUtils I find StringUtils.substringBefore() more readable than the alternatives:我发现StringUtils.substringBefore()比替代方案更具可读性:

String result = StringUtils.substringBefore("34.1 -118.33\n<!--ABCDEFG-->", "\n");

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.*表示“换行符和之后的所有内容”

How about怎么样

Scanner scanner = new Scanner(result);
String line = scanner.nextLine();//will contain 34.1 -118.33

You could use result = result.replaceAll("\\n","");您可以使用result = result.replaceAll("\\n",""); or要么

 String[] split = result.split("\n");

Try this:试试这个:

String result = "34.1 -118.33\n<!--ABCDEFG-->";
result = result.substring(0, result.indexOf("\n"));

使用Scanner并传入分隔符和原始字符串:

result = new Scanner(result).useDelimiter("\n").next();

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

This is the simplest method you can do and reduce your efforts.这是您可以做的最简单的方法,可以减少您的工作量。 just paste this function in your class and call it anywhere:只需将此函数粘贴到您的类中并在任何地方调用它:

you can do this by creating a substring.你可以通过创建一个子字符串来做到这一点。

simple exampe is here:简单的例子在这里:

 public static String removeTillWord(String input, String word) { return input.substring(input.indexOf(word)); } removeTillWord("Your String", "\\");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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