12

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

PersonName.setText(cursor.getString(cursor.getColumnIndex(cursor
                .getColumnName(1))).replaceAll("[^0-9]+"));

6 Answers 6

18

The correct RegEx for selecting all numbers would be just [0-9], you can skip the +, since you use 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]", "").

Sign up to request clarification or add additional context in comments.

Comments

12

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

    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);

1 Comment

What about consecutive occurrences of numbers? If the example is string1234value5678. Wouldn't \\d+ replace it to stringvalue in two iterations, instead of replacing it 8 times with \\d?
4

To remove the numbers, following code will do the trick.

 stringname.replaceAll("[0-9]","");

Comments

1

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

Comments

0

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.

try

replaceAll(<your regexp>,"")

Comments

0

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.

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.

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]", ""));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.