0

I'm trying to take this string

where lowercase(pdd.last_name) like 'albert%' 

and replace "pdd" with "p" using this function

sb.replace(queryAdd.indexOf("pdd"), (queryAdd.indexOf("pdd") + 3), "p");

where sb is a StringBuffer, but it doesn't work and here's what it actually returns

where lowercase(pdpast_name) like 'albert%' 

Why does this not replace "pdd" with "p"?

1
  • Yes i have. re-read the question please. Commented Mar 22, 2012 at 14:36

3 Answers 3

2
StringBuffer s = new StringBuffer("where lowercase(pdd.last_name) like 'albert%'");
    s.replace(s.indexOf("pdd"),(s.indexOf("pdd")+3), "p");
    System.out.println(s);

I think your sb and queryAdd have different data.

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

1 Comment

Oh god i think you're right. I shouldn't have been using my queryAdd var for the index, but my string buffer as you've suggested. I've been trying to get this to work for wayyy too long. Thanks.
1

Try this

String str = "where lowercase(pdd.last_name) like 'albert%'";
str = str.replaceFirst("pdd", "p");

Comments

1

It worked in my case. Have a look at my code (I am assuming this is what you are trying to do)....

StringBuffer sb = new StringBuffer("where lowercase(pdd.last_name) like 'albert%'");
        String queryAdd="where lowercase(pdd.last_name) like 'albert%'";
        sb.replace(queryAdd.indexOf("pdd"), (queryAdd.indexOf("pdd") + 3), "p");
        System.out.println(sb);

Output: where lowercase(p.last_name) like 'albert%'

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.