在Java中将字符插入字符串的最基本方法? [英] Most basic way to insert a character into a string in Java?

查看:93
本文介绍了在Java中将字符插入字符串的最基本方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个字符串x,我想增加一些字符x的次数,以使字符串x变成长度y,我该怎么做?

Say I have a string x and I want to add some character x amount of times so that string x becomes of length y, how would I do this?

String x = "this_is_a_line";
//x.length() = 14;
length y = 20;
//insert character into line so String x becomes balanced to:
x = "this___is___a___line";
//x.length() = 20;

另一个例子:

String y = "in_it_yeah";
//y.length() = 10
length j = 15;
//inserting characters so String y becomes balanced to:
y = "in____it___yeah";

我要避免使用StringBuilder附加字符。

I want to avoid using StringBuilder to append characters.

我的思维过程:


  1. 创建长度为y的Char数组。

  2. 复制

  3. 尝试将字符从最远的字符向右移一个


推荐答案

我这样做是因为,当我开始编写伪代码时,这对我来说有点挑战,尽管我不喜欢回答诸如此类的问题

I did this one because when I started doing a pseudo code it was a bit challenging for me, though I'm not a fan of answering questions like this on "gimme teh codez".

我建议您尽管在<$ c $内的字符串连接上使用 StringBuilder c> for 循环是因为它比使用 + = 的实际字符串连接更有效。

I recommend you though using StringBuilder on String concatenation inside for loops because it's more efficient than actual String concatenation with +=.

请注意:


  • 该程序在末尾添加空格,这就是为什么我打印长度被剪裁的原因。

  • 我用正则表达式 \\s + 而不是 _ 按空格将其拆分原因就是你写它的方式1

  • This program adds spaces to the end, that's why I print it's length trimmed.
  • I split it by space with a regex \\s+ instead of _ because that's how you wrote it 1st

以下代码适用于

a____b_c -> a___b___c (Lenght 9)
a____b_c -> a____b___c (Lenght 10)
this_is_a_line -> this___is___a___line (Lenght 20)
in_it_yeah -> in____it___yeah (Length 15)

代码:

class EnlargeString {
    public static void main (String args[]) {
        String x = "a    b c";
        int larger = 10;
        int numberOfSpaces = 0;
        String s[] = x.split("\\s+"); //We get the number of words

        numberOfSpaces = larger - s.length; //The number of spaces to be added after each token

        int extraSpaces = numberOfSpaces % s.length; //Extra spaces for the cases of 4 spaces then 3 or something like that

        System.out.println(extraSpaces);

        String newSpace[] = new String[s.length]; //The String array that will contain all string between words

        for (int i = 0; i < s.length; i++) {
            newSpace[i] = " "; //Initialize the previous array
        }

        //Here we add the extra spaces (the cases of 4 and 3)
        for (int i = 0; i < s.length; i++) {
            if (extraSpaces == 0) {
                break;
            }
            newSpace[i] += " ";
            extraSpaces--;
        }

        for (int i = 0; i < s.length; i++) {
            for (int j = 0; j < totalSpaces / s.length; j++) {
                newSpace[i] += " "; //Here we add all the equal spaces for all tokens
            }
        }

        String finalWord = "";
        for (int i = 0; i < s.length; i++) {
            finalWord += (s[i] + newSpace[i]); //Concatenate
        }
        System.out.println(x);
        System.out.println(x.length());
        System.out.println(finalWord);
        System.out.println(finalWord.trim().length());
    }
}

下次尝试自己做一次并显示<强大的逻辑,那么您的问题会更好地被接受(也许使用向上投票而不是向下投票),这被称为 Runnable示例。我还建议您查看字符串连接与StringBuilder 如何问一个好问题,您也可能想进行游览

Next time try to do it yourself 1st and show YOUR logic, then your question will be better received (maybe with upvotes instead of downvotes) this is called a Runnable Example. I also recommend you to take a look at String concatenation vs StringBuilder and How to ask a good question, also you might want to Take the tour

这篇关于在Java中将字符插入字符串的最基本方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆