How to insert Space after every Character of an existing String in Java?

前端 未结 9 1332
轻奢々
轻奢々 2020-12-03 11:01

I need to insert a space after every character of a string.

i.e. String name = \"Joe\";

should become: \"J o e\"

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 11:32

    You can convert Joe to char[] by using String's toCharArray() then traverse char[] to grab the char into another char[] and as you add the char to the second char[], you add a space character '" "'. Set a if-else within the loop to detect the last character so that you wouldn't add a space character by accident behind the last character. Use a String to valueOf() the resulting char[] to turn it into a String object.

提交回复
热议问题