1

I have an input string -

f.dollar_sales,f.unit_sales

I want to use String.replaceAll(regex,regex) method to get an output string as follows:

dollar_sales,unit_sales

I used the following:

fieldList.replaceAll("[a-zA-Z]\\Q.\\E"," ");

where fieldList is String variable where I've stored input String.

Can someone point out where I've made a mistake?

8
  • So you are trying to replace f.dollar_sales with just dollar_sales? Right? Commented Apr 6, 2015 at 16:31
  • Will the input string always contain, f. before the values you want, or will it always be a [a-z].? Do you need regular expression for this or would a simple substring suffice? Commented Apr 6, 2015 at 16:31
  • this works for me.. System.out.println(string.replaceAll("[a-zA-Z]\\Q.\\E", "")); replace the match with an empty string not with a space. Commented Apr 6, 2015 at 16:31
  • What is the result you got? And what were you expecting? Commented Apr 6, 2015 at 16:32
  • 1
    Sorry -an extremely stupid mistake - I printed value of fieldList. Instead of storing output by replaceAll method. Commented Apr 6, 2015 at 16:37

2 Answers 2

1

You need to assign your replaced string and \Q .. \E is not necessary here.

fieldList = fieldList.replaceAll("[a-zA-Z]\\.", "");

Ideone Demo

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

1 Comment

Yes this is the mistake OP is making +1
1

String is immutable, so you need to assign the updated string to that variable.

please run the below code;

public class StringReplace {
    public static void main(String[] args){
        String fieldList="f.dollar_sales,f.unit_sales";
        fieldList=fieldList.replaceAll("[a-zA-Z]\\Q.\\E"," ");
        System.out.println(fieldList);
    }
}

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.