1

I want to insert a % character before after every letter in a string, but using StringBuilder to make it fast.

For example, if a string is 'AA' then it would be '%A%A%'. If it is 'XYZ' then it would be '%X%Y%Z%'

1
  • It looks like you also insert a % character at the start of the string. Commented Feb 20, 2012 at 6:24

6 Answers 6

4
String foo = "VWXYZ";
foo = "%" + foo.replaceAll("(.)","$1%");
System.out.println(foo);

Output:

%V%W%X%Y%Z%

You don't need a StringBuilder. The compiler will take care of that simple concatenation prior to the regex for you by using one.

Edit in response to comment below:

replaceAll() uses a Regular Expression (regex).

The regex (.) says "match any character, and give me a reference to it" . is a wildcard for any character, the parenthesis create the backreference. The $1 in the second argument says "Use backreference #1 from the match".

replaceAll() keeps running this expression over the whole string replacing each character with itself followed by a percent sign, building a new String which it then returns to you.

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

1 Comment

This answer is a best answer, very good. @Brian Roach please explain more about your answer such as "(.)" or "$1%".
0

Try something like this:

    String test = "ABC";
    StringBuilder builder = new StringBuilder("");
    builder.append("%");
    for (char achar : test.toCharArray()) {
        builder.append(achar);
        builder.append("%");
    }
    System.out.println(builder.toString());

Comments

0
public static String escape(String s) {
    StringBuilder buf = new StringBuilder();
    boolean wasLetter = false;
    for (char c: s.toCharArray()) {
        boolean isLetter = Character.isLetter(c);
        if (isLetter && !wasLetter) {
            buf.append('%');
        }
        buf.append(c);
        if (isLetter) {
            buf.append('%');
        }
        wasLetter = isLetter;
    }
    return buf.toString();
}

Comments

0
StringBuilder sb = new StringBuilder("AAAAAAA");

for(int i = sb.length(); i >= 0; i--)
{
    sb.insert(i, '%');
}

1 Comment

Ignoring the fact that you really don't want to use a StringBuilder at all to do this, this is the worst performing approach using a StringBuilder. You're having to move data for every percent you're inserting. See the other answers that are using append()
0

You may see this.

    String s="AAAA";
    StringBuilder builder = new StringBuilder();
    char[] ch=s.toCharArray();
    for(int i=0;i<ch.length;i++)
    {
        builder.append("%"+ch[i]);
    }
    builder.append("%");
    System.out.println(builder.toString());

Output

    %A%A%A%A%

Comments

0

I agree with @Brian Roach to add character to before and after but if you want to add any specific character then do like this

String source = "hello good old world";
StringBuffer res = new StringBuffer();
String[] strArr = tagList.split(" ");

for (String str : strArr) {
     char[] stringArray = str.trim().toCharArray();
     stringArray[0] = stringArray[0];
     str = new String(stringArray);
     //here you need to specify your first and last character which you want to set 
     res.append("#"+ str + "$").append(" ");
 }

 System.out.println("Result: " + res.toString().trim());

Output :- #hello$ #good$ #old$ #world$

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.