简体   繁体   English

如何在不使用字符串缓冲区类replace()方法的情况下替换字符串中的字符?

[英]How can I replace characters in a string without using String Buffer class replace() method?

I need to make replace method that replaces the characters between start(inclusive) and end(exclusive, ie characters up to index end-1 will be replaced) in this TextLine with characters in the specified string fragment. 我需要使用该指定字符串片段中的字符,来替换此TextLine中start(包含)和end(不包含,即直到索引end-1的字符)之间的字符的replace方法。 I can not directly or indirectly use StringBuffer Class replace(int start, int end, String fragment) method. 我不能直接或间接使用StringBuffer类的replace(int start,int end,Stringfragment)方法。 I'm trying to make the eLine.replace(0, 3, "abc"); 我正在尝试使eLine.replace(0,3,“ abc”); or eLine.replace(0, 3, "abc"); 或eLine.replace(0,3,“ abc”); work. 工作。

I tried to make a replace method similar to StringBuffer Class but it didn't work out. 我试图制作一个类似于StringBuffer类的替换方法,但是没有成功。 I can't think of another way to make a replace like that which is why I'm stuck. 我想不出另一种替代方法,这就是为什么我被困住了。 If there is another way, please show me an example or solution. 如果还有其他方法,请向我展示示例或解决方案。

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”

For an isolated method to take any string and make the replacement, this would be the easiest. 对于一个孤立的方法来获取任何字符串并进行替换,这将是最简单的。

   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;
   }

I intentionally omitted using StringBuilder . 我故意省略使用StringBuilder This should be easy to adapt to a homegrown "String" class. 这应该很容易适应本地的“ String”类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用.replace()的情况下替换字符串中的某些字符? - How do I replace certain characters in a String without using .replace()? 在Java中不使用replace方法替换字符串中的特定字符 - Replace specific characters in a string without using replace method in Java 如何在不使用 replace 方法的情况下替换用户输入的字符串中的字符? - How can I replace a character in a string that the user inputs without using the replace method? 如何在不使用.replace的情况下替换Java中字符串中的字符? - How to replace characters in a string in Java without using .replace? 使用 java 替换方法替换字符串中的字符 - Replace characters within a string using the java replace method 替换字符串中的字符,否则 - Replace characters in string without if else 字符串替换方法不替换字符 - String replace method is not replacing characters 如何替换 Java 中字符串中的某些字符? - How can I replace certain characters within a String in Java? 如何使用String中的字符替换数字和特殊字符? - How can we replace the digits and special characters using the character in String? 如何在不使用 replace() 方法、StringBuilder、StringBuffer 或数组的情况下替换字符串中的所有字符? - How to replace all characters in a String without using replace() methods , StringBuilder, or StringBuffer, or Arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM