简体   繁体   English

如何在StringBuilder中连接子字符串?

[英]How to concatenate substring in StringBuilder?

How can I refactor substring concatenations to StringBuilder ? 如何将子字符串串联重构为StringBuilder

        String line = "this is my start and my end string";
        int start = line.indexOf("start");
        line = line.substring(0, start) + line.substring(start + 5);

        int end = line.indexOf("end");
        line = line.substring(0, end) + line.substring(end + 3);
            //result: this is my and my string";

How would the same code look like with StringBuilder? 使用StringBuilder,相同的代码看起来如何? When I want to execute substrings several times one after the other? 当我想一次又一次地执行子字符串几次时?

If your question is "How do I repeatedly remove substrings from a string", you might try something like this: 如果您的问题是“如何重复从字符串中删除子字符串”,则可以尝试以下操作:

StringBuilder line = new StringBuilder("this is my start and my end string");
int start = line.indexOf("start");
line.delete(start, start + 5);
int end = line.indexOf("end");
line.delete(end, end + 3);
String result = sb.toString()

Create the StringBuilder once, and then manipulate its contents. 一次创建StringBuilder,然后操纵其内容。

Something like this may be: 这样的事情可能是:

    StringBuilder line = new StringBuilder("this is my **start** and my **end** string");
    int start = line.indexOf("start");
    line = new StringBuilder(line.substring(0, start).append(line.substring(start + 5)));

    int end = line.indexOf("end");
    line = new StringBuilder(line.substring(0, end).append(line.substring(end + 3)));

Something like this you mean? 像这样的东西吗? Or have I understood it wrong? 还是我理解错了?

String line = "this is my start and my end string";
int start = line.indexOf("start");
int end = line.indexOf("end");
StringBuilder sb = new StringBuilder();
sb.append(line.substring(0, start);
sb.append(line.substring(start + 5);
sb.append(line.substring(0, end);
sb.append(line.substring(end + 3);
String result = sb.toString()

Try 尝试

String line = "this is my start and my end string";
    int start = line.indexOf("start");
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(line.substring(0, start));
    stringBuilder.append(line.substring(start + 5));

    int end = line.indexOf("end");
    stringBuilder.append(line.substring(0, end));
    stringBuilder.append(line.substring(end + 3));
    System.out.println("StringBuilder : "+stringBuilder.toString());

StringBuilder has it's Own indexOf and substring implementations if i am not wrong. 如果我没有记错的话,StringBuilder有它自己的indexOf和子字符串实现。 The primary concern of the question seems to discard the use of String, and main features of StringBuilder are still concatenations. 这个问题的主要关注点似乎是放弃使用String,而StringBuilder的主要功能仍然是串联。

So http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html can help you find relevant methods, and you can go on using StringBuilder with .append() method and ur string finding and substring methods. 因此http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html可以帮助您找到相关方法,并且可以继续将StringBuilder与.append()方法和字符串查找一起使用和子字符串方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM