Replace certain string in array of strings

后端 未结 7 906
广开言路
广开言路 2021-01-12 22:07

let\'s say I have this string array in java

String[] test = {"hahaha lol", "jeng jeng jeng", &quo         


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 22:29

    You can use IntStream instead. Code might look something like this:

    String[] test = {"hahaha lol", "jeng jeng jeng", "stack overflow"};
    
    IntStream.range(0, test.length).forEach(i ->
            // replace non-empty sequences
            // of whitespace characters
            test[i] = test[i].replaceAll("\\s+", "%20"));
    
    System.out.println(Arrays.toString(test));
    // [hahaha%20lol, jeng%20jeng%20jeng, stack%20overflow]
    

    See also: How to replace a whole string with another in an array

提交回复
热议问题