Replace certain string in array of strings

后端 未结 7 991
广开言路
广开言路 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:42

    Iterate over the Array and replace each entry with its encoded version.

    Like so, assuming that you are actually looking for URL-compatible Strings only:

    for (int index =0; index < test.length; index++){
      test[index] = URLEncoder.encode(test[index], "UTF-8");
    }
    

    To conform to current Java, you have to specify the encoding - however, it should always be UTF-8.

    If you want a more generic version, do what everyone else suggests:

    for (int index =0; index < test.length; index++){
        test[index] = test[index].replace(" ", "%20");
    }
    

提交回复
热议问题