Java 如何将字符串替换为一个字符集

Java 如何将字符串替换为一个字符集,java,string,for-loop,extract,string-length,Java,String,For Loop,Extract,String Length,如何将输入的字符串替换为一个字符集: 例如: 字符串是:Hello Word 输出应为:aaaaaaaa 空格应保持为空格。 我试过这个,但是没有空间了 Scanner input = new Scanner(System.in); String word = input.nextLine(); for(int i = 0; i < word.length(); i++){ System.out.print("a"); } 扫描仪输入=新扫描

如何将输入的字符串替换为一个字符集: 例如:

字符串是:Hello Word 输出应为:aaaaaaaa

空格应保持为空格。

我试过这个,但是没有空间了

Scanner input = new Scanner(System.in);
String word =  input.nextLine();

for(int i = 0; i < word.length(); i++){                 
    System.out.print("a");
}
扫描仪输入=新扫描仪(System.in);
String word=input.nextLine();
对于(inti=0;i
扫描仪输入=新扫描仪(System.in);
String word=input.nextLine();
对于(inti=0;i
基本上,当遇到空格时,您希望跳过System.out.print(“a”)。

扫描仪输入=新扫描仪(System.in);
String word=input.nextLine();
对于(inti=0;i
基本上,当遇到空格时,您希望跳过System.out.print(“a”)。

也许

System.out.println("Hello Word".replaceAll("\\S", "a"));
或许

System.out.println("Hello Word".replaceAll("\\S", "a"));

你可以这样做:

for(int i = 0; i < word.length(); i++){
   System.out.print(word.charAt(i) == ' ' ? ' ' : 'a');
}
public static void main(String[] args) {
    String word = "Hello World!";
    String replacedWord = "";

    for (char c : word.toCharArray()) {
        if (c != ' ') {
            replacedWord += 'a';
        } else {
            replacedWord += c;
        }
    }

    System.out.println(word + " --> " + replacedWord);
}
或者您可以采用Java-8方式:

System.out.println(word.chars()
            .mapToObj(c -> ((char)c) == ' ' ? " " : "a")
            .collect(joining()));

你可以这样做:

for(int i = 0; i < word.length(); i++){
   System.out.print(word.charAt(i) == ' ' ? ' ' : 'a');
}
public static void main(String[] args) {
    String word = "Hello World!";
    String replacedWord = "";

    for (char c : word.toCharArray()) {
        if (c != ' ') {
            replacedWord += 'a';
        } else {
            replacedWord += c;
        }
    }

    System.out.println(word + " --> " + replacedWord);
}
或者您可以采用Java-8方式:

System.out.println(word.chars()
            .mapToObj(c -> ((char)c) == ' ' ? " " : "a")
            .collect(joining()));

此方法将把任何字符串替换为除空格外所需的任何字符

    public static void main(String []args) {
        String t = "Hello Word";
        replace(t, 'a');
    }

    public static void replace(String word, char wanted) {
        for (int i = 0; i < word.length(); i++){
            System.out.print((word.charAt(i) == ' ' ? " " : wanted));
        }
    }
publicstaticvoidmain(字符串[]args){
String t=“Hello Word”;
替换(t,'a');
}
公共静态void替换(字符串字,需要字符){
for(int i=0;i
此方法将把任何字符串替换为除空格外所需的任何字符

    public static void main(String []args) {
        String t = "Hello Word";
        replace(t, 'a');
    }

    public static void replace(String word, char wanted) {
        for (int i = 0; i < word.length(); i++){
            System.out.print((word.charAt(i) == ' ' ? " " : wanted));
        }
    }
publicstaticvoidmain(字符串[]args){
String t=“Hello Word”;
替换(t,'a');
}
公共静态void替换(字符串字,需要字符){
for(int i=0;i
迭代
word
中的每个字符,如果是空白,则不加修改地打印。否则,请打印
a
。像

for (char ch : word.toCharArray()) {
    if (Character.isWhitespace(ch)) {
        System.out.print(ch);
    } else {
        System.out.print('a');
    }
}

迭代
word
中的每个字符,如果是空白,则不加修改地打印。否则,请打印
a
。像

for (char ch : word.toCharArray()) {
    if (Character.isWhitespace(ch)) {
        System.out.print(ch);
    } else {
        System.out.print('a');
    }
}

或者,您可以像这样迭代
word.tocharray()

for(int i = 0; i < word.length(); i++){
   System.out.print(word.charAt(i) == ' ' ? ' ' : 'a');
}
public static void main(String[] args) {
    String word = "Hello World!";
    String replacedWord = "";

    for (char c : word.toCharArray()) {
        if (c != ' ') {
            replacedWord += 'a';
        } else {
            replacedWord += c;
        }
    }

    System.out.println(word + " --> " + replacedWord);
}

或者,您可以像这样迭代
word.tocharray()

for(int i = 0; i < word.length(); i++){
   System.out.print(word.charAt(i) == ' ' ? ' ' : 'a');
}
public static void main(String[] args) {
    String word = "Hello World!";
    String replacedWord = "";

    for (char c : word.toCharArray()) {
        if (c != ' ') {
            replacedWord += 'a';
        } else {
            replacedWord += c;
        }
    }

    System.out.println(word + " --> " + replacedWord);
}

在这一点上,为什么不使用lambdas添加一个解决方案呢

public String goofy(String str) {
    return Stream.of(str.split("")).map(ch -> {
            if (ch.equals(" "))
                return " ";
            else
                return "a";
}).collect(Collectors.joining(""));
}

顺便说一句,最实用的解决方案是@Reimeus提供的,它让我期待已久。

那么,现在为什么不使用lambdas添加一个解决方案呢

public String goofy(String str) {
    return Stream.of(str.split("")).map(ch -> {
            if (ch.equals(" "))
                return " ";
            else
                return "a";
}).collect(Collectors.joining(""));
}

顺便说一句,最实用的解决方案是@Reimeus提供的,它让我期待已久。

我们应该使用代码点而不是字符

System.out.println(
        "Hello, world!".codePoints()
        .mapToObj(Character::isWhiteSpace)
        .map(Map.of(true, " ", false, "a")::get)  // yeah I know, but just to be goofy
        .collect(Collectors.joining()));

我们应该使用代码点而不是字符

System.out.println(
        "Hello, world!".codePoints()
        .mapToObj(Character::isWhiteSpace)
        .map(Map.of(true, " ", false, "a")::get)  // yeah I know, but just to be goofy
        .collect(Collectors.joining()));
请尝试以下代码:

Scanner input=new Scanner(System.in);
String word=input.nextLine();
String x[] =word.split(" ");
int count=x.length;

for(int i=0;i<count;i++){
    for(int j=0;j<=x[i].length();j++){
       if(j==x[i].length()){
           System.out.print(" ");
       }else{
           System.out.print("a");
       }
    }
}
扫描仪输入=新扫描仪(System.in);
String word=input.nextLine();
字符串x[]=word.split(“”);
int count=x.length;
对于(int i=0;i请尝试以下代码:

Scanner input=new Scanner(System.in);
String word=input.nextLine();
String x[] =word.split(" ");
int count=x.length;

for(int i=0;i<count;i++){
    for(int j=0;j<=x[i].length();j++){
       if(j==x[i].length()){
           System.out.print(" ");
       }else{
           System.out.print("a");
       }
    }
}
扫描仪输入=新扫描仪(System.in);
String word=input.nextLine();
字符串x[]=word.split(“”);
int count=x.length;
对于(int i=0;i请尝试以下方法:

for(int i = 0; i < word.length(); i++) {
    if(word.charAt(i) != ' ') {
        System.out.print("a");
    }
    System.out.print(" ");
}
for(int i=0;i
试试这个:

for(int i = 0; i < word.length(); i++) {
    if(word.charAt(i) != ' ') {
        System.out.print("a");
    }
    System.out.print(" ");
}
for(int i=0;i
OP说“空间应该仍然是一个空间。”OP说“空间应该仍然是一个空间”。您可以将代码减少到
返回array.stream(str.split(“”)。map(ch->ch.equals(“”):“a”).collect(Collectors.joining())
。注意使用
数组.stream
这是从现有数组(split
方法返回的数组)创建流时的惯用方法,而不是使用
stream.of
创建“从显式键入的值流”。此外,在
加入
收集器中,当它是
时,您不需要指定分隔符。“
只需不传递一个分隔符即可。否则好主意:)你说得对,我在函数中间用了一个长而朴素的说教版本,逻辑堆栈。但是你完全正确。你可以做的非常短,只要用一个int流通过Strug.CARS方法以一个铸造为代价,你就可以把代码减少一点到<代码>返回数组。(str.split(“”).map(ch->ch.equals(“”?):“a”).collect(Collectors.joining());
。请注意使用
数组.stream
,这是从现有数组(split
方法返回)创建流时的惯用方法与用于“从显式键入的值创建流”的
Stream.of
相反。此外,在
加入
收集器时,您不需要指定分隔符。“
只需完全不传递一个。否则好主意:)你说得对,我在函数中间用了一个长而朴素的说教版本,逻辑堆栈。但是你完全正确。你可以做的非常非常短,只要以一个int值与Stcink。