Java 在不使用正则表达式或StringBuilder的情况下,每5个字符放置一个空格

Java 在不使用正则表达式或StringBuilder的情况下,每5个字符放置一个空格,java,string,Java,String,我想让用户在scanner类中输入一个句子。 确保过滤掉所有空格(例如:thisatest变成thisatest) 然后用for循环打印出这个句子,每5个字符有一个空格 (例如,这是一种状态) 这就是我目前所拥有的 import java.util.Scanner; public class BlockText { public static void main(String[] args) { Scanner s = new Scanner(System.in);

我想让用户在scanner类中输入一个句子。 确保过滤掉所有空格(例如:
thisatest
变成
thisatest
) 然后用for循环打印出这个句子,每5个字符有一个空格 (例如,
这是一种状态

这就是我目前所拥有的

import java.util.Scanner;

public class BlockText {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Give your sentence: ");
        String sentence = s.nextLine();
        String nospace = sentence.replace(" ", "");
        String out = "";       

        for (int i = 5; i < nospace.length(); i+=5) {

            out += nospace.replaceAll( nospace.substring(i) , " ");        
        }   
        System.out.println("Sentence without spaces: " + nospace);
        System.out.println("This gives: " + out);
    }

}

有人能帮我吗?正如我在
titel
中所说的,我希望通过for循环实现这一点,而不使用regex或StringBuilder

这里确实应该使用
StringBuilder
,因为在循环中附加带有
+=
的字符串效率非常低

如果没有字符串生成器,您可以执行以下操作:

private static String addSpacesEvery5(String s) {
    String out = "";
    for (int i = 0 ; i < s.length() ; i++) {
        if (i % 5 == 0 && i != 0) {
            out += " "; // this will run once every five iterations, except the first one
        }
        out += s.charAt(i);
    }
    return out;
}
Scanner s = new Scanner(System.in);
System.out.println("Give your sentence: ");
String sentence = s.nextLine();
String nospace = sentence.replace(" ", "");
String out = addSpacesEvery5(nospace);
System.out.println("Sentence without spaces: " + nospace);
System.out.println("This gives: " + out);
然后您可以在
main
方法中使用它,如下所示:

private static String addSpacesEvery5(String s) {
    String out = "";
    for (int i = 0 ; i < s.length() ; i++) {
        if (i % 5 == 0 && i != 0) {
            out += " "; // this will run once every five iterations, except the first one
        }
        out += s.charAt(i);
    }
    return out;
}
Scanner s = new Scanner(System.in);
System.out.println("Give your sentence: ");
String sentence = s.nextLine();
String nospace = sentence.replace(" ", "");
String out = addSpacesEvery5(nospace);
System.out.println("Sentence without spaces: " + nospace);
System.out.println("This gives: " + out);
使用字符串生成器,
addSpacesEvery5
可以重写为:

private static String addSpacesEvery5(String s) {
    StringBuilder out = new StringBuilder();
    for (int i = 0 ; i < s.length() ; i++) {
        if (i % 5 == 0 && i != 0) {
            out.append(" ");
        }
        out.append(s.charAt(i));
    }
    return out.toString();
}
private静态字符串addSpacesEvery5(字符串s){
StringBuilder out=新的StringBuilder();
对于(int i=0;i
这里有一个相对简单且更快的方法:

      String tmp = new String();

      int len = str.length();

      int remOdds = len % 5;

      int i = 0;

      while (i < len - remOdds)
      {
         tmp = tmp.concat(str.substring(i, i + 5));
         i += 5;
         if (i < len)
         {
            tmp += " ";
         }
      }

      while (i < len)
      {
         tmp += str.charAt(i);
         i++;
      }

      str = tmp;
String tmp=new String();
int len=str.length();
int remOdds=长度%5;
int i=0;
而(i
replaceAll
使用正则表达式。可能您的意思是
replace
@Pshemo well replace,或者如果有其他方法可以每隔5个字符放置一个空格。另外
out+=nospace…
内部使用
StringBuilder
,但效率非常低。这项任务的要点是创建大小合适的
char[]
数组(您可以根据非空格字符的数量计算需要多少额外的空格),并用相应的字符填充它。然后,您可以使用
新字符串(charArray)
创建结果
字符串
。对于5个字符长的输入,会发生什么情况?比如“你好”。输出应该是“hello”(带尾随空格),还是仅仅是“hello”?(无试用空间)这是学校作业,我们还不允许使用数组。谢谢你,这对我帮助很大,这是我所需要的。我刚才做了,谢谢你告诉我,我还是这个网站的初学者。“没有字符串生成器”
out+=foo将被编译为
out=newStringBuilder(out).append(foo).toString()(或类似的东西),所以它并不是真正避免
StringBuilder