如何替换java字符串中的所有数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23607583/
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 replace all numbers in java string
提问by skyshine
I have string like this String s="ram123",d="ram varma656887"
I want string like ram and ram varma so how to seperate string from combined string
I am trying using regex but it is not working
我有这样的字符串 Strings="ram123",d="ram varma656887"
我想要像 ram 和 ram varma 这样的字符串所以如何从组合字符串中分离字符串我正在尝试使用正则表达式,但它不起作用
PersonName.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(1))).replaceAll("[^0-9]+"));
采纳答案by Leandros
The correct RegEx for selecting all numbers would be just [0-9]
, you can skip the +
, since you use replaceAll
.
用于选择所有数字的正确 RegEx 只是[0-9]
,您可以跳过+
,因为您使用replaceAll
.
However, your usage of replaceAll
is wrong, it's defined as follows: replaceAll(String regex, String replacement)
. The correct code in your example would be: replaceAll("[0-9]", "")
.
但是,您的用法replaceAll
是错误的,它的定义如下:replaceAll(String regex, String replacement)
. 在你的榜样,正确的代码是:replaceAll("[0-9]", "")
。
回答by Spring Breaker
To remove the numbers, following code will do the trick.
要删除数字,以下代码将起作用。
stringname.replaceAll("[0-9]","");
回答by Hymany Cheng
i think you missed the second argument of replace all. You need to put a empty string as argument 2 instead of actually leaving it empty.
我认为您错过了全部替换的第二个论点。您需要将一个空字符串作为参数 2,而不是实际将其留空。
try
尝试
replaceAll(<your regexp>,"")
回答by anirudh
You can use the following regex: \d
for representing numbers. In the regex that you use, you have a ^
which will check for any characters other than the charset 0-9
您可以使用以下正则表达式:\d
表示数字。在您使用的正则表达式中,您有一个^
将检查字符集以外的任何字符0-9
String s="ram123";
System.out.println(s);
/* You don't need the + because you are using the replaceAll method */
s = s.replaceAll("\d", ""); // or you can also use [0-9]
System.out.println(s);
回答by Jitesh Upadhyay
Please do as follows
请按以下步骤操作
String name = "ram varma656887";
name = name.replaceAll("[0-9]","");
System.out.println(name);//ram varma
alternatively you can do as
或者你可以这样做
String name = "ram varma656887";
name = name.replaceAll("\d","");
System.out.println(name);//ram varma
also something like given will work for you
也像给定的东西对你有用
String given = "ram varma656887";
String[] arr = given.split("\d");
String data = new String();
for(String x : arr){
data = data+x;
}
System.out.println(data);//ram varma
回答by MacDaddy
you can use Java - String replaceAll() Method. This method replaces each substring of this string that matches the given regular expression with the given replacement.
您可以使用 Java - String replaceAll() 方法。此方法用给定的替换替换此字符串中与给定正则表达式匹配的每个子字符串。
Here is the syntax of this method:
这是此方法的语法:
public String replaceAll(String regex, String replacement)
Here is the detail of parameters:
下面是参数的详细信息:
regex-- the regular expression to which this string is to be matched.
regex-- 与此字符串匹配的正则表达式。
replacement-- the string which would replace found expression.
替换-- 将替换找到的表达式的字符串。
Return Value:
返回值:
This method returns the resulting String.
此方法返回结果字符串。
for your question use this
对于您的问题,请使用此
String s = "ram123", d = "ram varma656887";
System.out.println("s" + s.replaceAll("[0-9]", ""));
System.out.println("d" + d.replaceAll("[0-9]", ""));