0

How to add whichever character between every character of a premade String? (JAVA)

For example, I have the String "Hello world" and I have to add '_' between every character of the String.

Any function or useful code I can use to do it?

I have to do an algorithm that make me output "H_e_l_l_o_ _w_o_r_l_d"

This is what I have:

public String example(String s) {
        String s2 = null;
        for(int i = 0; i < s.length(); i++){
            s2 += s.charAt(i) + (((i+1) == 0) ? " " : "-");     
            }
        return s2;
     }

My output in the main class is being:

nullH-e-l-l-o- -w-o-r-l-d-

Don't know why

Angel
  • 11
  • 3
  • 1
    So what did you try? – Roman C Feb 10 '16 at 18:55
  • Welcome to StackOverflow! In order to help you with your problem, please post a minimum, complete example, and then ask which part you are having trouble with. http://stackoverflow.com/help/mcve – gariepy Feb 10 '16 at 19:03
  • public String example(String s) { String s2 = null; for(int i = 0; i < s.length(); i++){ s2 += s.charAt(i) + (((i+1) == 0) ? " " : "-"); } return s2; } – Angel Feb 10 '16 at 19:11
  • So the obvious solution is don't start with null: `String s2 = null;`. Instead start with a non-null empty String `String s2 = "";` – Hovercraft Full Of Eels Feb 10 '16 at 19:16
  • Closed as a duplicate. – Hovercraft Full Of Eels Feb 10 '16 at 19:18
  • Yes, I realized it out. This was my first message in Stack Overflow, didn't know people will answer so fast. – Angel Feb 10 '16 at 19:20
  • package temp; public class Temp { public String strReplace(String s) { String s2=""; StringBuilder s3=null; for(int i = 0; i < s.length(); i++){ s2= s2+s.charAt(i) + (((i+1) == 0) ? " " : "-"); } s3 = new StringBuilder(s2); s3.deleteCharAt(s3.length()-1); return s3.toString(); } public static void main(String[] args) { Temp t = new Temp(); String s1 = t.strReplace("helloworld"); System.out.println(s1); } } – Shek Feb 10 '16 at 19:42

1 Answers1

1

This looks like a homework assignment. So, I won't directly write out all the code.

String = "hello world";

Say, there is a variable len = str.length() - 1. Instead of doing it from index 0, we will start our for loop from len - 1. The character 'd' is at index len, and the '_' will have to be inserted right before that. This can be done by setting the string to str = str.substring(0,i) + "_" + str.substring(i+1);

You will have to use a for loop that starts from len - 1 and goes on till the index reached is 0.

Now, on every single iteration, when you are inserting a character assigning str to str.substring(0,i) + "_" + str.substring(i+1); causes you to make a new string object, which is absolutely horrible style. This can be solved by using a StringBuilder.

Does that make it clear?

In the future, refrain from posting questions without having done any work. This community is there to help you with solving issues that you may have in your solutions, not write your solutions for you.

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43