martinseal1987
martinseal1987

Reputation: 2419

Replace characters in string, but only after a certain point in the string

I have a huge string I made from a list, I want to replace every ',' with a new line, but only after a certain string that starts with "http". I was using .replace(",","\n") but this replaces every one so I need a kind of while loop like this

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

then I need to maybe create substrings and check if it has http inside before replacing, I'm no expert and I'm sure there is an easier way.

Upvotes: 0

Views: 1114

Answers (4)

daniu
daniu

Reputation: 14999

So to implement my previous comment about working on the original list instead of creating the huge String first.

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

This will do the replacement while you're constructing the "huge String" so it should be much more efficient. It does however require "http" to be present in each item for the rest to be replaced; if it occurs only once overall, you need to keep track of whether it occurred at an earlier time, like this:

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

If the List you're constructing from contains Strings to begin with, you can leave the T and convertToString stuff out and just do

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

Upvotes: 0

HadiMohammadi
HadiMohammadi

Reputation: 133

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

result:

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

Upvotes: 4

slawek
slawek

Reputation: 338

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

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

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

And output

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

Upvotes: 2

sellc
sellc

Reputation: 378

This should do the trick. Be careful with how the data is being read. If it's read line by line then the http check will be properly met. Otherwise it'll be one long string and every ',' will be replaced. If you want to read text in the same format as your example. Search for the "http" string and then create a substring from that index. Then run the if statement below.

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

Upvotes: 1

Related Questions