java - 如何在不使用 String Buffer 类的 Replace() 方法的情况下替换字符串中的字符?

标签 java

我需要创建替换方法,用指定字符串片段中的字符替换此 TextLine 中开始(包含)和结束(不包含,即索引 end-1 之前的字符)之间的字符。我不能直接或间接使用StringBuffer类的replace(int start, int end, Stringfragment)方法。我正在尝试制作 eLine.replace(0, 3, "abc");或 eLine.replace(0, 3, "abc");工作。

我尝试制作一个类似于 StringBuffer 类的替换方法,但没有成功。我想不出另一种方法来进行类似的替换,这就是我陷入困境的原因。如果有其他方法,请给我一个例子或解决方案。

public int length;
public char[] characters;

public class TextLineTester {
  public static void main(String args[]) { 
     Scanner input = new Scanner(System.in);
     System.out.println("Enter a line of text.");
     String text = input.nextLine();
     EditableTextLine eLine = new EditableTextLine(text);
     Scanner strCharsInput = new Scanner(System.in);
     System.out.println("Enter string of characters.");
     String str = strCharsInput.nextLine();
     eLine.replace(0, 3, "abc");
     eline.replace(0, str.length(), "abc"); // suppose to replace all occurrences of string eLine with the string ”abc”and print the modified eLine
     System.out.println(eLine.toString());
  }  
}

public void replace(int start, int end, String fragment) {
     if (end > length) {
        end = length;
     }

     int fragmentLength = fragment.length();
     int newLength = length + fragmentLength - (end - start);
     ensureCapacityInternal(newLength);
     System.arraycopy(characters, end, characters, start + 
                                           fragmentLength, length - end);
     fragment.getChars(0,0, characters, start);
     length = newLength;
}

public EditableTextLine(String line) { // creates EditableTextLine object
   length = line.length();
   characters = new char[DEFAULT_SIZE * 2];
   characters = line.toCharArray();
}

public String toString() {
   return "Characters: " + new String(characters);
}

}

This is the error I get from this current replace method. 
  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at edu.uga.cs1302.txtbuff.EditableTextLine.replace(EditableTextLine.java:109)
    at edu.uga.cs1302.test.TextLineTester.main(TextLineTester.java:36)


 Input: ABCDEFG
 After  eLine.replace(0, 3, "abc"), Output will be 
 Output: abcBCDEFG


 Another example:
 Input: AB678CDEFGHIJK12345
 eLine.replace(2,5,”XY”);  // line is now ”ABXYCDEFGHIJK12345”

最佳答案

对于采用任何字符串并进行替换的隔离方法,这将是最简单的。

   public static String replace(String orig, int start, int end, String replace) {
      String front = orig.substring(0,start);
      String back = orig.substring(end);
      return front + replace + back;
   }

我故意省略了StringBuilder的使用。这应该很容易适应本土的“String”类。

关于java - 如何在不使用 String Buffer 类的 Replace() 方法的情况下替换字符串中的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56962111/

相关文章:

Java - 获取原始数据类型的变量类型

java - 对象列表和Gson字符串之间的转换

java - 无法加载资源:net::ERR_FILE_NOT_FOUND 但数据已加载

java - 将 JavaMail 与 Google AppEngine 一起使用时出现异常 : How to fix this?

Java 解析 truetype 字体以将每个字符提取为图像及其代码

java - 从第三方框架中过滤掉 log4j 消息?

java - 如何将 deeplearning4j Word2vec 与 Spark 结合使用?

Java:如果枚举常量本质上是静态的,它如何能够有一个构造函数和与之关联的方法

java - 是否可以使用java中的apache tika从excel表(从列或行)中获取特定数据?

java - Android - 事件队列 - 在首选项设置后执行