在Java中的字符串中的所有字母之前和之后插入一个字符

发布于 2025-01-07 00:23:08 字数 128 浏览 0 评论 0原文

我想在字符串中的每个字母之后插入一个 % 字符,但使用 StringBuilder 来加快速度。

例如,如果字符串是“AA”,那么它将是“%A%A%”。如果是 'XYZ' 那么它将是 '%X%Y%Z%'

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

瀟灑尐姊 2025-01-14 00:23:08
String foo = "VWXYZ";
foo = "%" + foo.replaceAll("(.)","$1%");
System.out.println(foo);

输出:

%V%W%X%Y%Z%

您不需要 StringBuilder。编译器将通过使用一个来为您处理正则表达式之前的简单串联。

编辑以响应以下评论:

replaceAll() 使用 正则表达式(regex)

正则表达式 (.) 表示“匹配任何字符,并给我一个对它的引用” . 是任何字符的通配符,括号创建反向引用。第二个参数中的 $1 表示“使用匹配中的反向引用 #1”。

replaceAll() 在整个字符串上不断运行此表达式,将每个字符替换为自身后跟百分号,构建一个新字符串,然后将其返回给您。

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.

笑梦风尘 2025-01-14 00:23:08

尝试这样的事情:

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

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());
情绪失控 2025-01-14 00:23:08
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();
}
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();
}
感情旳空白 2025-01-14 00:23:08
StringBuilder sb = new StringBuilder("AAAAAAA");

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

for(int i = sb.length(); i >= 0; i--)
{
    sb.insert(i, '%');
}
流殇 2025-01-14 00:23:08

你可能会看到这个。

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

输出

    %A%A%A%A%

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%
梦里南柯 2025-01-14 00:23:08

我同意 @Brian Roach 在前后添加字符,但如果您想添加任何特定字符,请执行以下操作像这样

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

输出:- #hello$ #good$ #old$ #world$

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$

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文