如何删除Java中特定字符之前的所有字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40910779/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:59:34  来源:igfitidea点击:

How to remove all characters before a specific character in Java?

javastringreplace

提问by Shariq Musharaf

I have a string and I'm getting value through a html form so when I get the value it comes in a URL so I want to remove all the characters before the specific charater which is =and I also want to remove this character. I only want to save the value that comes after =because I need to fetch that value from the variable..

我有一个字符串,我通过 html 表单获取值,所以当我获取值时,它出现在 URL 中,所以我想删除特定字符之前的所有字符=,我也想删除这个字符。我只想保存后面的值,=因为我需要从变量中获取该值..

EDIT : I need to remove the =too since I'm trying to get the characters/value in string after it...

编辑:我也需要删除它,=因为我试图在它之后获取字符串中的字符/值......

采纳答案by ItamarG3

You can use .substring():

您可以使用.substring()

String s = "the text=text";
String s1 = s.substring(s.indexOf("=")+1);
s1.trim();

then s1contains everything after =in the original string.

然后s1包含=原始字符串中的所有内容。

s1.trim()

s1.trim()

.trim()removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).

.trim()删除字符串的第一个字符(不是空格,例如字母、数字等)之前的空格(前导空格),并删除最后一个字符之后的空格(尾随空格)。

回答by Arjun Mehta

You can split the string from the = and separate in to array and take the second value of the array which you specify as after the = sign For example:

您可以将字符串从 = 拆分并分隔到数组中,并在 = 符号之后取数组的第二个值,例如:

String CurrentString = "Fruit = they taste good"; String[] separated = CurrentString.split("="); separated[0]; // this will contain "Fruit" separated[1]; //this will contain "they teste good"

String CurrentString = "Fruit = they taste good"; String[] separated = CurrentString.split("="); separated[0]; // this will contain "Fruit" separated[1]; //this will contain "they teste good"

then separated[1] contains everything after = in the original string.

然后 separator[1] 包含原始字符串中 = 之后的所有内容。

回答by Murat Karag?z

While there are many answers. Here is a regex example

虽然有很多答案。这是一个正则表达式示例

String test = "eo21jüdjüqw=realString";
test = test.replaceAll(".+=", "");
System.out.println(test);

// prints realString

Explanation:

解释:

.+matches any character (except for line terminators)
+Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
=matches the character = literally (case sensitive)

.+匹配任何字符(行终止符除外)
+量词 — 在一次和无限次之间匹配,尽可能多次,根据需要返回(贪婪)
=匹配字符 = 字面意思(区分大小写)

This is also a shady copy paste from https://regex101.com/where you can try regex out.

这也是来自https://regex101.com/的阴暗复制粘贴,您可以在其中尝试正则表达式。

回答by yogur

Maybe locate the first occurrence of the character in the URL String. For Example:

也许在 URL 字符串中找到该字符的第一次出现。例如:

String URL = "http://test.net/demo_form.asp?name1=stringTest";

int index = URL.indexOf("=");

Then, split the String based on an index

然后,根据索引拆分字符串

String Result = URL.substring(index+1); //index+1 to skip =

String Result now contains the value: stringTest

字符串结果现在包含值:stringTest

回答by Sir Codesalot

I know this is asked about Javabut this seems to also be the first search result for Kotlinso you should know that Kotlinhas the String.substringAfter(delimiter: String, missingDelimiterValue: String = this)extension for this case.

我知道有人问过这个问题,Java但这似乎也是第一个搜索结果,Kotlin因此您应该知道该案例Kotlin具有String.substringAfter(delimiter: String, missingDelimiterValue: String = this)扩展名。

Its implementation is:

它的实现是:

val index = indexOf(delimiter)
return if (index == -1) 
    missingDelimiterValue 
else 
    substring(index + delimiter.length, length)

回答by рüффп

If you use the Apache Commons Lang3 library, you can also use the substringAftermethod of the StringUtilsutility class.

如果使用 Apache Commons Lang3 库,也可以使用实用程序类的substringAfter方法StringUtils

Official documentation is here

官方文档在这里

Examples:

例子:

String value = StringUtils.substringAfter("key=value", "=");   

// in this case where a space is in the value (e.g. read from a file instead of a query params)
String value = StringUtils.trimToEmpty(StringUtils.substringAfter("key = value", "="));   // = "value"

It manage the case where your values can contains the '=' character as it takes the first occurence.

它管理您的值可以包含 '=' 字符的情况,因为它需要第一次出现。

If you have keys and values also containing '=' character it will not work (but the other methods as well); in the URL query params, such a character should be escaped anyway.

如果您的键和值也包含 '=' 字符,它将不起作用(但其他方法也是如此);在 URL 查询参数中,无论如何都应该转义这样的字符。