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

Java 替换字符串中的字符,但仅在字符串中的某一点之后替换,java,regex,string,replace,Java,Regex,String,Replace,我从列表中创建了一个巨大的字符串,我想用新行替换每个“,”,但只在以http开头的某个字符串之后。我使用的是.replace,,\n但是这会替换所有的,所以我需要一种类似这样的while循环 i = str.indexOf(','); while(i >= 0) { System.out.println(i); i = str.indexOf(',', i+1); } 然后我可能需要创建子字符串,并在替换之前检查它是否包含http,我不是专家,我相信有一种更简单的方法。这应该可

我从列表中创建了一个巨大的字符串,我想用新行替换每个“,”,但只在以http开头的某个字符串之后。我使用的是.replace,,\n但是这会替换所有的,所以我需要一种类似这样的while循环

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

然后我可能需要创建子字符串,并在替换之前检查它是否包含http,我不是专家,我相信有一种更简单的方法。

这应该可以做到。请注意如何读取数据。如果逐行读取,则http检查将得到正确满足。否则它将是一个长字符串,并且每个“,”将被替换。如果您想阅读与示例相同格式的文本。搜索http字符串,然后根据该索引创建子字符串。然后运行下面的if语句

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

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

aa.com,bb.com, B C
d

因此,为了实现我之前关于处理原始列表而不是首先创建巨大字符串的评论

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

可能str=str.replaceAllhttp[^,]*,$1\n?首先提取相关部分。因此,搜索http字符串并在其中拆分,例如使用Stringsubstring和StringindexOf。在那之后,使用常规的replaceAll方法。@Zabuza这将不起作用,我将以相同的结果结束,想象一个字符串嗨,你好,你好吗,http//myurl.com,再见,很高兴见到你,http//otherurl.com,我只希望它替换出现在http@WiktorStribi呃,我一定要试试这个谢谢,我将评论soonHow如何处理原始列表而不是首先创建巨大的字符串?您应该使用s.startsWith。是的,使用startWith更好。我改进我的回答。然而,似乎OPs输入可以有多个http引用,它应该只替换引用之间部分的内容,请参见注释。因此,拆分然后奇数索引或多个indexOf调用应该可以做到这一点。
    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);
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();
}
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();
}
public static String toHugeStringReplacingCommas(List<String> list) {
    StringBuilder builder = new StringBuilder();
    for (String string : list) {
        // and so on