2

for example:

String str = "abc1234abcd12";
String str1 = "abc1234abcd1234abcd12";

adding a '-', after third char, and add '-', after every 4 char

how can I get this output:

String str = "abc-1234-abcd-12";
String str1 = "abc-1234-abcd-1234-abcd-12";
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
skyy tan
  • 37
  • 2
  • 1
    You can use a `StringBuilder`. – cs95 Jun 25 '17 at 14:07
  • 1
    By the way, that isn't "every 4"... – cs95 Jun 25 '17 at 14:09
  • @Coldspeed No...he wants three characters in the very beginning, four in every other occurrence. – Tim Biegeleisen Jun 25 '17 at 14:10
  • Welcome to Stack Overflow! Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Jun 25 '17 at 14:40
  • this is a NOT a "write my program for me, send me teh codez" site. This should be down voted mercilessly and closed and everyone that answers this should be down voted into oblivion! Just wait until this type of person is your co-worked and you have to do all their work they get the money and credit and then end up your manager, you will be getting what you deserve! –  Jun 25 '17 at 14:41
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. –  Jun 25 '17 at 14:43
  • Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. –  Jun 25 '17 at 14:43
  • 1
    @JarrodRoberson I disagreed with you that no one should answer, until you mentioned the part about that person becoming my manager. This is exactly how I would describe most of my software managers actually; taking credit for others work. – Tim Biegeleisen Jun 25 '17 at 14:44

3 Answers3

-1

You can even get the formatting you want using a one-liner, q.v. the code snippet below. I manually build the first three characters followed a dash. After this, I do a blanket replacement to insert dashes after each subsequent group of four characters at at time. My code assumes that your string is at least three characters long. If you have logic for what happens for strings less than three characters of length, then update your question with that information.

String str1 = "abc1234abcd1234abcd12";
str1 = str1.substring(0, 3) + "-" + str1.substring(3).replaceAll("(.{4})", "$1-");
System.out.println(str1);

Output:

abc-1234-abcd-1234-abcd-12

Demo here:

Rextester

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
-1

This is the larger but better explainable solution for school ;).

String str = "abc1234abcd1234abcd12";
StringBuilder sb = new StringBuilder();
sb.append(str.substring(0,3));
for (int i = 3; i < str.length(); i = i + 4) {
    int start = i;
    int end = i + 4 > str.length() ? str.length() : i + 4;
    sb.append("-" + str.substring(start, end));
}
System.out.println(sb.toString());
Unkall
  • 29
  • 1
  • 6
-2

how can I get this output:...

It looks like you need to put a separator - to place apart a numbers from letter..

using java 8 can be as simple as:

String s = "abc1234abcd1234abcd12";
// split to numbers and letters using regex
String[] res = s.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
// join that using the "-"
String finalResult = String.join("-", res);
System.out.println(finalResult);

the output:

abc-1234-abcd-1234-abcd-12

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97