Hunt
Hunt

Reputation: 8435

Insert a character before and after all letters in a string in Java

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%'

Upvotes: 1

Views: 5465

Answers (6)

AiVision
AiVision

Reputation: 4233

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$

Upvotes: 0

Brian Roach
Brian Roach

Reputation: 76908

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.

Upvotes: 4

Biswas
Biswas

Reputation: 127

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%

Upvotes: 0

Atrus
Atrus

Reputation: 798

StringBuilder sb = new StringBuilder("AAAAAAA");

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

Upvotes: 0

Maurice Perry
Maurice Perry

Reputation: 32831

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

Upvotes: 0

fyr
fyr

Reputation: 20859

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

Upvotes: 0

Related Questions