替换字符串中的字符,但仅在字符串中的某个点之后

时间:2018-01-21 18:02:04

标签: java regex string replace

我从列表中创建了一个巨大的字符串,我想用新行替换每个','但只能在以“http”开头的某个字符串之后。我正在使用.replace(",","\n"),但这取代了每一个,所以我需要一种像这样的while循环

i = str.indexOf(',');
while(i >= 0) {
  System.out.println(i);
  i = str.indexOf(',', i+1);
} 

然后我需要创建子串并在替换之前检查它是否有内部http,我不是专家,我确​​信有一种更简单的方法。

4 个答案:

答案 0 :(得分:4)

    String inputStr = "aa.com,bb.com,http://cc.com,b,c,d";
    int httpIndex = inputStr.indexOf("http");
    String result = inputStr.substring(0,httpIndex) + inputStr.substring(httpIndex).replaceAll(",", "\n");
    System.out.println("result = " + result);

<强>结果:

aa.com,bb.com,http://cc.com
b
ç
d

答案 1 :(得分:2)

String s = "http://side.de/servet/patth?jjjj,qwe,rtz,zui,opl";

if ( s.startsWith("http")) {

    s = s.replaceAll(",", "\n");
    System.out.println(s);;
}

输出

http://side.de/servet/patth?jjjj
qwe
rtz
zui
opl

答案 2 :(得分:1)

这应该可以解决问题。小心如何读取数据。如果逐行读取,则将正确满足http检查。否则它将是一个长字符串,每个','将被替换。如果您想以与示例相同的格式阅读文本。搜索“http”字符串,然后从该索引创建子字符串。然后运行下面的if语句。

if (s.contains("http")) {
    s = s.replace(",", "\n");
}

答案 3 :(得分:0)

所以要实现我之前关于处理原始列表的注释,而不是先创建巨大的String。

static final Pattern PATTERN = Pattern.compile("(.*http)(.*)");

public static <T> String toHugeStringReplacingCommas(List<T> list, Function<T, String> convertToString) {
    // we're collecting a lot of Strings, a StringBuilder is most efficient
    StringBuilder builder = new StringBuilder();
    for (T item : list) {
        String string = convertToString(item);
        Matcher m = PATTERN.matcher(string);
        if (m.isMatch(string)) {
            // everything up to and including "http"
            StringBuilder replaced = new StringBuilder(m.groups(1));
            replaced.append(m.groups(2).replaceAll(",", "\n"));
            string = replaced.toString();
        }
        builder.append(string);
    }
    return builder.toString();
}

这将在您构建“巨大字符串”时进行替换,因此它应该更加高效。然而,它确实需要在每个项目中存在“http”以便替换其余部分;如果它只发生一次,你需要跟踪它是否发生在较早的时间,如下所示:

public static <T> String toHugeStringReplacingCommas(List<T> list, Function<T, String> convertToString) {
    StringBuilder builder = new StringBuilder();
    boolean httpFound = false;
    for (T item : list) {
        String string = convertToString(item);
        if (!httpFound) {
            Matcher m = PATTERN.matcher(string);
            httpFound = m.isMatch(string);
            if (httpFound) {
                // we found the first occurance of "http"
                // append the part up to http without replacing,
                // leave the replacing of the rest to be done outside the loop
                builder.append(m.groups(1));
                string = m.groups(2);
            }
        }
        if (httpFound) {
            string = string.replaceAll(",", "\n");
        }
        builder.append(string);
    }
    return builder.toString();
}

如果您构建的List包含字符串开头,您可以将TconvertToString内容保留,只需执行

public static String toHugeStringReplacingCommas(List<String> list) {
    StringBuilder builder = new StringBuilder();
    for (String string : list) {
        // and so on