Format a string into a specific format
Currently i have a 9 String number
String s = 123456789
how can i convert this string into this kind of format?
String newS = 12 - 34 - 5678 - 9
java string
add a comment |
Currently i have a 9 String number
String s = 123456789
how can i convert this string into this kind of format?
String newS = 12 - 34 - 5678 - 9
java string
4
which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?
– Vladyslav Matviienko
Nov 13 '18 at 7:17
1
are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.
– Gautham M
Nov 13 '18 at 7:49
add a comment |
Currently i have a 9 String number
String s = 123456789
how can i convert this string into this kind of format?
String newS = 12 - 34 - 5678 - 9
java string
Currently i have a 9 String number
String s = 123456789
how can i convert this string into this kind of format?
String newS = 12 - 34 - 5678 - 9
java string
java string
edited Nov 13 '18 at 7:38
Hayk Abelyan
216314
216314
asked Nov 13 '18 at 7:13
netflix spotifynetflix spotify
587
587
4
which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?
– Vladyslav Matviienko
Nov 13 '18 at 7:17
1
are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.
– Gautham M
Nov 13 '18 at 7:49
add a comment |
4
which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?
– Vladyslav Matviienko
Nov 13 '18 at 7:17
1
are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.
– Gautham M
Nov 13 '18 at 7:49
4
4
which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?
– Vladyslav Matviienko
Nov 13 '18 at 7:17
which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?
– Vladyslav Matviienko
Nov 13 '18 at 7:17
1
1
are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.
– Gautham M
Nov 13 '18 at 7:49
are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.
– Gautham M
Nov 13 '18 at 7:49
add a comment |
4 Answers
4
active
oldest
votes
Try this one :
public static String getFormattedAccountNumber(String result){
StringBuilder stringBuilder;
if (result.length() > 3 && result.length() < 13) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(3, "-");
result = stringBuilder.toString();
if (result.length() > 6 && result.length() < 14) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(6, "-");
result = stringBuilder.toString();
if (result.length() > 13 && result.length() < 15) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(13, "-");
result = stringBuilder.toString();
}
}
}
return result;
}
add a comment |
I hope this will help you.
public static String formatString(String str) {
return str.substring(0, 1) + " - " + str.substring(2, 3) + " - " + str.substring(4, 7) + " - " + str.substring(8);
}
add a comment |
With Java8 streams you can get a cleaner version...
String s = "123456789";
AtomicInteger pos = new AtomicInteger();
String newS = IntStream.of(2, 2, 4, 1)
.mapToObj(n -> s.substring(pos.getAndAdd(n), pos.get()))
.collect(Collectors.joining(" - "));
notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...
– rifaqat
Nov 13 '18 at 8:14
add a comment |
You can use a regular expression:
String s = "123456789";
Pattern pattern = Pattern.compile("(\d{2})(\d{2})(\d{4})(\d)");
Matcher matcher = pattern.matcher(s);
String formatted = matcher.find() ?
matcher.group(1) + " - " + matcher.group(2) + " - " + matcher.group(3) + " - " + matcher.group(4) :
"";
System.out.println(formatted); // 12 - 34 - 5678 - 9
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275687%2fformat-a-string-into-a-specific-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this one :
public static String getFormattedAccountNumber(String result){
StringBuilder stringBuilder;
if (result.length() > 3 && result.length() < 13) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(3, "-");
result = stringBuilder.toString();
if (result.length() > 6 && result.length() < 14) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(6, "-");
result = stringBuilder.toString();
if (result.length() > 13 && result.length() < 15) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(13, "-");
result = stringBuilder.toString();
}
}
}
return result;
}
add a comment |
Try this one :
public static String getFormattedAccountNumber(String result){
StringBuilder stringBuilder;
if (result.length() > 3 && result.length() < 13) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(3, "-");
result = stringBuilder.toString();
if (result.length() > 6 && result.length() < 14) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(6, "-");
result = stringBuilder.toString();
if (result.length() > 13 && result.length() < 15) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(13, "-");
result = stringBuilder.toString();
}
}
}
return result;
}
add a comment |
Try this one :
public static String getFormattedAccountNumber(String result){
StringBuilder stringBuilder;
if (result.length() > 3 && result.length() < 13) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(3, "-");
result = stringBuilder.toString();
if (result.length() > 6 && result.length() < 14) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(6, "-");
result = stringBuilder.toString();
if (result.length() > 13 && result.length() < 15) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(13, "-");
result = stringBuilder.toString();
}
}
}
return result;
}
Try this one :
public static String getFormattedAccountNumber(String result){
StringBuilder stringBuilder;
if (result.length() > 3 && result.length() < 13) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(3, "-");
result = stringBuilder.toString();
if (result.length() > 6 && result.length() < 14) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(6, "-");
result = stringBuilder.toString();
if (result.length() > 13 && result.length() < 15) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(13, "-");
result = stringBuilder.toString();
}
}
}
return result;
}
answered Nov 13 '18 at 7:22
FreelancsAndroidLovesyouFreelancsAndroidLovesyou
159112
159112
add a comment |
add a comment |
I hope this will help you.
public static String formatString(String str) {
return str.substring(0, 1) + " - " + str.substring(2, 3) + " - " + str.substring(4, 7) + " - " + str.substring(8);
}
add a comment |
I hope this will help you.
public static String formatString(String str) {
return str.substring(0, 1) + " - " + str.substring(2, 3) + " - " + str.substring(4, 7) + " - " + str.substring(8);
}
add a comment |
I hope this will help you.
public static String formatString(String str) {
return str.substring(0, 1) + " - " + str.substring(2, 3) + " - " + str.substring(4, 7) + " - " + str.substring(8);
}
I hope this will help you.
public static String formatString(String str) {
return str.substring(0, 1) + " - " + str.substring(2, 3) + " - " + str.substring(4, 7) + " - " + str.substring(8);
}
edited Nov 13 '18 at 7:57
oleg.cherednik
6,39621118
6,39621118
answered Nov 13 '18 at 7:50
macmurimacmuri
644
644
add a comment |
add a comment |
With Java8 streams you can get a cleaner version...
String s = "123456789";
AtomicInteger pos = new AtomicInteger();
String newS = IntStream.of(2, 2, 4, 1)
.mapToObj(n -> s.substring(pos.getAndAdd(n), pos.get()))
.collect(Collectors.joining(" - "));
notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...
– rifaqat
Nov 13 '18 at 8:14
add a comment |
With Java8 streams you can get a cleaner version...
String s = "123456789";
AtomicInteger pos = new AtomicInteger();
String newS = IntStream.of(2, 2, 4, 1)
.mapToObj(n -> s.substring(pos.getAndAdd(n), pos.get()))
.collect(Collectors.joining(" - "));
notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...
– rifaqat
Nov 13 '18 at 8:14
add a comment |
With Java8 streams you can get a cleaner version...
String s = "123456789";
AtomicInteger pos = new AtomicInteger();
String newS = IntStream.of(2, 2, 4, 1)
.mapToObj(n -> s.substring(pos.getAndAdd(n), pos.get()))
.collect(Collectors.joining(" - "));
With Java8 streams you can get a cleaner version...
String s = "123456789";
AtomicInteger pos = new AtomicInteger();
String newS = IntStream.of(2, 2, 4, 1)
.mapToObj(n -> s.substring(pos.getAndAdd(n), pos.get()))
.collect(Collectors.joining(" - "));
answered Nov 13 '18 at 8:12
rifaqatrifaqat
14617
14617
notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...
– rifaqat
Nov 13 '18 at 8:14
add a comment |
notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...
– rifaqat
Nov 13 '18 at 8:14
notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...
– rifaqat
Nov 13 '18 at 8:14
notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...
– rifaqat
Nov 13 '18 at 8:14
add a comment |
You can use a regular expression:
String s = "123456789";
Pattern pattern = Pattern.compile("(\d{2})(\d{2})(\d{4})(\d)");
Matcher matcher = pattern.matcher(s);
String formatted = matcher.find() ?
matcher.group(1) + " - " + matcher.group(2) + " - " + matcher.group(3) + " - " + matcher.group(4) :
"";
System.out.println(formatted); // 12 - 34 - 5678 - 9
add a comment |
You can use a regular expression:
String s = "123456789";
Pattern pattern = Pattern.compile("(\d{2})(\d{2})(\d{4})(\d)");
Matcher matcher = pattern.matcher(s);
String formatted = matcher.find() ?
matcher.group(1) + " - " + matcher.group(2) + " - " + matcher.group(3) + " - " + matcher.group(4) :
"";
System.out.println(formatted); // 12 - 34 - 5678 - 9
add a comment |
You can use a regular expression:
String s = "123456789";
Pattern pattern = Pattern.compile("(\d{2})(\d{2})(\d{4})(\d)");
Matcher matcher = pattern.matcher(s);
String formatted = matcher.find() ?
matcher.group(1) + " - " + matcher.group(2) + " - " + matcher.group(3) + " - " + matcher.group(4) :
"";
System.out.println(formatted); // 12 - 34 - 5678 - 9
You can use a regular expression:
String s = "123456789";
Pattern pattern = Pattern.compile("(\d{2})(\d{2})(\d{4})(\d)");
Matcher matcher = pattern.matcher(s);
String formatted = matcher.find() ?
matcher.group(1) + " - " + matcher.group(2) + " - " + matcher.group(3) + " - " + matcher.group(4) :
"";
System.out.println(formatted); // 12 - 34 - 5678 - 9
answered Nov 13 '18 at 8:16
LuCioLuCio
2,7641823
2,7641823
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275687%2fformat-a-string-into-a-specific-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?
– Vladyslav Matviienko
Nov 13 '18 at 7:17
1
are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.
– Gautham M
Nov 13 '18 at 7:49