I'm trying to create a method where the currentString is returned with underscores in the middle of each character. For example, if the current string is: "hi there" the output string would be h_i__t_h_e_r_e with no underscore at the beginning or end. I'm aware this can be done with regex and replaceAll, but I'm trying to accomplish this with a for loop instead
My current code looks like this:
public String spacedWord()
{
String spacedString = "";
for (int i = 0; i<currentString.length(); i++)
{
spacedString = spacedString+currentString.charAt(i)+"_";
}
return spacedString;
}
If the current string is "hey there" my code returns h_e_y_ _t_h_e_r_e_ with an underscore at the end.
The code returns properly with underscores in between each character, but I'm not sure how make sure an underscore doesn't appear at the end of the string.
Any help is appreciated!
The "-1" trick.