What's the best way to insert a -
(dash/minus character) after every 8 characters in a Java String, starting from the right?
Examples:
1111 -> 1111
111111111 -> 1-11111111
1111111111111111 -> 11111111-11111111
100001111111111111111 -> 10000-11111111-11111111
My attempt, to show that I have tried doing it myself (a comment below asks: "is this homework?":
import junit.framework.TestCase;
public class InsertCharacterAfterEveryNCharacters extends TestCase {
public static String insertSpacerAfterNCharactersFromTheRight(char spacer,
int spacing, String string) {
final int length = string.length();
final int newStringCapacity = length + (int) Math.ceil(length / (double) spacing);
StringBuilder stringBuilder = new StringBuilder(newStringCapacity);
for (int i = length - 1; i >= 0; i--) {
stringBuilder.append(string.charAt(i));
if (i % spacing == 0 && i > 0) {
stringBuilder.append(spacer);
}
}
return stringBuilder.toString();
}
public static void testInsertSpacerAfterNCharactersFromTheRight() {
assertEquals("", insertSpacerAfterNCharactersFromTheRight('-', 8, ""));
assertEquals("1", insertSpacerAfterNCharactersFromTheRight('-', 8, "1"));
assertEquals("11", insertSpacerAfterNCharactersFromTheRight('-', 8, "11"));
assertEquals("11111111",
insertSpacerAfterNCharactersFromTheRight('-', 8, "11111111"));
assertEquals("1-11111111",
insertSpacerAfterNCharactersFromTheRight('-', 8, "111111111"));
assertEquals("11111111-11111111",
insertSpacerAfterNCharactersFromTheRight('-', 8, "1111111111111111"));
}
}
You can make use of String#substring() . String newstring = string. substring(0, 1) + "-" + string. substring(1);
Example: One can add character at the start of String using the '+' operator.
A simple solution to append a character to the end of a string is using the string concatenation operator (+) . This creates a new instance of the string, since strings in Java are immutable and cannot be modified.
All answers seem a bit lot of code for what needs to be done. You could use a regular expression to do that.
Let's say you have a method that returns your formatted string.
A simple, clear example:
String myString = "00000000000111111111111100000000001111111000011000000";
String newString = myString.replaceAll("(.{8})(?!$)", "$1-");
return newString;
Above is equal to the following shorter notation:
return myString.replaceAll("(.{8})(?!$)", "$1-");
Another similar, short notation (in case of a fixed, hard-coded string):
return "00000000000111111111111100000000001111111000011000000".replaceAll("(.{8})(?!$)", "$1-");
Each piece of code resturns the following string:
00000000-00011111-11111111-00000000-00111111-10000110-00000
For more info on regular expressions, see for example http://www.regular-expressions.info/java.html
Hope this helps anyone in the future.
Edit note: The last group doesn;t have 8 characters. However, if it would, it won't add another dash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With