Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java string, insert a dash after every 8 characters, starting from the right [duplicate]

Tags:

java

string

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"));
  }
}
like image 436
Robottinosino Avatar asked Aug 15 '12 17:08

Robottinosino


People also ask

How do you add a hyphen to a string in Java?

You can make use of String#substring() . String newstring = string. substring(0, 1) + "-" + string. substring(1);

How do you add a character to the beginning of a string in Java?

Example: One can add character at the start of String using the '+' operator.

How do you add a character to the end of a string in Java?

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.


1 Answers

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.

like image 129
ar34z Avatar answered Oct 08 '22 21:10

ar34z