Java 如何在不使用replace()的情况下将一个字符替换为另一个字符

Java 如何在不使用replace()的情况下将一个字符替换为另一个字符,java,Java,我不允许使用replace()。我只能使用substring、indexOf和length()。如何使用这些方法替换字符?这是我尝试过的,但不起作用 public static String replace(String s) { int b = 0; String result = ""; int index = s.indexOf(' '); while(index != -1) { result += s.substring(b,index) + '\n';

我不允许使用replace()。我只能使用substring、indexOf和length()。如何使用这些方法替换字符?这是我尝试过的,但不起作用

public static String replace(String s)
{
  int b = 0;
  String result = "";
  int index = s.indexOf(' ');

  while(index != -1)
  {
     result += s.substring(b,index) + '\n';
     s = s.substring(index+1);
     index = s.indexOf(' ');
  }

  return result;
}   

***更正:我取出
b=索引 您需要将以前的
索引
存储在
b
中。此外,您还可以使用
indexOf()
的第二个参数来控制您在
字符串中的位置。像

public static String replace(String s) {
    int b = 0;
    String result = "";
    int index = s.indexOf(' ');
    if (index > -1) {
        while (index != -1) {
            result += s.substring(b, index) + '\n';
            // s = s.substring(index + 1);
            b = index;
            index = s.indexOf(' ', index + 1);
        }
        result += s.substring(b + 1);
    } else {
        result = s;
    }
    return result;
}

您可以在不使用indexOf和substring的情况下执行此操作。
实现取自java.lang.String.replace()

publicstaticstringreplace(stringstr、charoldchar、charnewchar){
if(oldChar!=newChar){
char[]value=str.toCharArray();
int len=value.length;
int i=-1;
char[]val=值;
而(++i
您可以这样做,除非您必须使用您提到的所有方法。无论如何,这可能是值得思考的

  public static String replace(String s) {
    String[] split = s.split("");
    String result = "";
    for (int i = 0; i < split.length; i++) {
      if (split[i].equals(" ")) {
        split[i] = "\n";
      }
      result+=split[i];
    }


    return result;
  }
公共静态字符串替换(字符串s){
字符串[]split=s.split(“”);
字符串结果=”;
对于(int i=0;i
您还可以使用递归方法解决该任务:

public static void main(String[] args) {
    System.out.println(replace("a b c ..."));
}

public static String replace(final String str) {
    final int index = str.indexOf(' '); // find the first occurence
    if (index == - 1) { // if there are no whitespaces ...
        return str; // ... then return the unchanged String
    }
    // cut off the part before the whitespace, append \n to it and then append
    // the result of another "replace" call with the part after the found whitespace
    return String.format("%s%s%s",
        str.substring(0, index), "\n", replace(str.substring(index + 1)));
}

代码中的注释应该描述递归方法的行为。如果您对此有疑问,请留言。

您想做什么?将
'
替换为
'\n'
?是。带有新行字符的空格这是一种奇怪的家庭作业吗?目的是什么?你打算从中学习什么?一个更简单的解决方案是循环字符串,并测试每个字符是否为空。如果是这样,请在结果字符串中附加一个换行符,否则字符本身就可以了。在当前代码中返回result+s就可以了。
public static void main(String[] args) {
    System.out.println(replace("a b c ..."));
}

public static String replace(final String str) {
    final int index = str.indexOf(' '); // find the first occurence
    if (index == - 1) { // if there are no whitespaces ...
        return str; // ... then return the unchanged String
    }
    // cut off the part before the whitespace, append \n to it and then append
    // the result of another "replace" call with the part after the found whitespace
    return String.format("%s%s%s",
        str.substring(0, index), "\n", replace(str.substring(index + 1)));
}