how to make multiple line string to single line string?
I have below string
String str="select * from m_menus;
select * from m_roles";
I want above string in one line
like
String str="select * from m_menus;select * from m_roles";
I have tried
str1=str.replace("[rn]+", " ");
and also
str1=str.replace("n"," ");
Both are not working.
java java-11
add a comment |
I have below string
String str="select * from m_menus;
select * from m_roles";
I want above string in one line
like
String str="select * from m_menus;select * from m_roles";
I have tried
str1=str.replace("[rn]+", " ");
and also
str1=str.replace("n"," ");
Both are not working.
java java-11
2
Stupid question.. but why don't you just do this in the first place?:String str="select * from m_menus;select * from m_roles";Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.
– forsvarir
Jun 27 '12 at 7:43
@forsvarir Probably he is getting string in that manner from user input?
– Mukund Samant
Jun 27 '12 at 7:44
I am taking string as input from user
– happy
Jun 27 '12 at 7:53
add a comment |
I have below string
String str="select * from m_menus;
select * from m_roles";
I want above string in one line
like
String str="select * from m_menus;select * from m_roles";
I have tried
str1=str.replace("[rn]+", " ");
and also
str1=str.replace("n"," ");
Both are not working.
java java-11
I have below string
String str="select * from m_menus;
select * from m_roles";
I want above string in one line
like
String str="select * from m_menus;select * from m_roles";
I have tried
str1=str.replace("[rn]+", " ");
and also
str1=str.replace("n"," ");
Both are not working.
java java-11
java java-11
edited Jan 2 at 6:05
Niraj Sonawane
2,42121943
2,42121943
asked Jun 27 '12 at 7:39
happyhappy
912154279
912154279
2
Stupid question.. but why don't you just do this in the first place?:String str="select * from m_menus;select * from m_roles";Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.
– forsvarir
Jun 27 '12 at 7:43
@forsvarir Probably he is getting string in that manner from user input?
– Mukund Samant
Jun 27 '12 at 7:44
I am taking string as input from user
– happy
Jun 27 '12 at 7:53
add a comment |
2
Stupid question.. but why don't you just do this in the first place?:String str="select * from m_menus;select * from m_roles";Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.
– forsvarir
Jun 27 '12 at 7:43
@forsvarir Probably he is getting string in that manner from user input?
– Mukund Samant
Jun 27 '12 at 7:44
I am taking string as input from user
– happy
Jun 27 '12 at 7:53
2
2
Stupid question.. but why don't you just do this in the first place?:
String str="select * from m_menus;select * from m_roles"; Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.– forsvarir
Jun 27 '12 at 7:43
Stupid question.. but why don't you just do this in the first place?:
String str="select * from m_menus;select * from m_roles"; Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.– forsvarir
Jun 27 '12 at 7:43
@forsvarir Probably he is getting string in that manner from user input?
– Mukund Samant
Jun 27 '12 at 7:44
@forsvarir Probably he is getting string in that manner from user input?
– Mukund Samant
Jun 27 '12 at 7:44
I am taking string as input from user
– happy
Jun 27 '12 at 7:53
I am taking string as input from user
– happy
Jun 27 '12 at 7:53
add a comment |
6 Answers
6
active
oldest
votes
If you want to use regex, you should use the String.replaceAll() method.
2
Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.
– TZHX
Jun 27 '12 at 7:47
add a comment |
Use String.replaceAll instead.
str1=str.replaceAll("[rn]+", " ");
add a comment |
Why don't you use str.replaceAll("rn", " ") ?
Should work and replace all occurences.
add a comment |
No regular expressions and operating system independent:
str1.replaceAll(System.lineSeparator(), " ");
Windows uses rn as a line breaker, while *nix systems use only n.
add a comment |
Using Java 11 lines()
Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string
yourMultilineString.lines().collect(Collectors.joining(";"));
Note The string uses Windows’ rn and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats r, n, and rn as line terminators and splits there – even if they are mixed in the same string.
add a comment |
str1=str.replaceAll("[rn]+", " ");
try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer
– Agilanbu
Jan 2 at 6:37
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%2f11221491%2fhow-to-make-multiple-line-string-to-single-line-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to use regex, you should use the String.replaceAll() method.
2
Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.
– TZHX
Jun 27 '12 at 7:47
add a comment |
If you want to use regex, you should use the String.replaceAll() method.
2
Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.
– TZHX
Jun 27 '12 at 7:47
add a comment |
If you want to use regex, you should use the String.replaceAll() method.
If you want to use regex, you should use the String.replaceAll() method.
answered Jun 27 '12 at 7:44
TZHXTZHX
3,814103850
3,814103850
2
Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.
– TZHX
Jun 27 '12 at 7:47
add a comment |
2
Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.
– TZHX
Jun 27 '12 at 7:47
2
2
Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.
– TZHX
Jun 27 '12 at 7:47
Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.
– TZHX
Jun 27 '12 at 7:47
add a comment |
Use String.replaceAll instead.
str1=str.replaceAll("[rn]+", " ");
add a comment |
Use String.replaceAll instead.
str1=str.replaceAll("[rn]+", " ");
add a comment |
Use String.replaceAll instead.
str1=str.replaceAll("[rn]+", " ");
Use String.replaceAll instead.
str1=str.replaceAll("[rn]+", " ");
answered Jun 27 '12 at 7:44
Mattias BuelensMattias Buelens
15.4k42946
15.4k42946
add a comment |
add a comment |
Why don't you use str.replaceAll("rn", " ") ?
Should work and replace all occurences.
add a comment |
Why don't you use str.replaceAll("rn", " ") ?
Should work and replace all occurences.
add a comment |
Why don't you use str.replaceAll("rn", " ") ?
Should work and replace all occurences.
Why don't you use str.replaceAll("rn", " ") ?
Should work and replace all occurences.
answered Jun 27 '12 at 7:44
Michael LaffargueMichael Laffargue
8,63843264
8,63843264
add a comment |
add a comment |
No regular expressions and operating system independent:
str1.replaceAll(System.lineSeparator(), " ");
Windows uses rn as a line breaker, while *nix systems use only n.
add a comment |
No regular expressions and operating system independent:
str1.replaceAll(System.lineSeparator(), " ");
Windows uses rn as a line breaker, while *nix systems use only n.
add a comment |
No regular expressions and operating system independent:
str1.replaceAll(System.lineSeparator(), " ");
Windows uses rn as a line breaker, while *nix systems use only n.
No regular expressions and operating system independent:
str1.replaceAll(System.lineSeparator(), " ");
Windows uses rn as a line breaker, while *nix systems use only n.
answered Mar 14 '18 at 7:50
dfinkidfinki
197111
197111
add a comment |
add a comment |
Using Java 11 lines()
Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string
yourMultilineString.lines().collect(Collectors.joining(";"));
Note The string uses Windows’ rn and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats r, n, and rn as line terminators and splits there – even if they are mixed in the same string.
add a comment |
Using Java 11 lines()
Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string
yourMultilineString.lines().collect(Collectors.joining(";"));
Note The string uses Windows’ rn and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats r, n, and rn as line terminators and splits there – even if they are mixed in the same string.
add a comment |
Using Java 11 lines()
Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string
yourMultilineString.lines().collect(Collectors.joining(";"));
Note The string uses Windows’ rn and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats r, n, and rn as line terminators and splits there – even if they are mixed in the same string.
Using Java 11 lines()
Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string
yourMultilineString.lines().collect(Collectors.joining(";"));
Note The string uses Windows’ rn and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats r, n, and rn as line terminators and splits there – even if they are mixed in the same string.
answered Jan 2 at 6:04
Niraj SonawaneNiraj Sonawane
2,42121943
2,42121943
add a comment |
add a comment |
str1=str.replaceAll("[rn]+", " ");
try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer
– Agilanbu
Jan 2 at 6:37
add a comment |
str1=str.replaceAll("[rn]+", " ");
try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer
– Agilanbu
Jan 2 at 6:37
add a comment |
str1=str.replaceAll("[rn]+", " ");
str1=str.replaceAll("[rn]+", " ");
edited Jan 2 at 6:40
iOS
2,79722048
2,79722048
answered Jan 2 at 6:15
joslin selva cjoslin selva c
113
113
try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer
– Agilanbu
Jan 2 at 6:37
add a comment |
try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer
– Agilanbu
Jan 2 at 6:37
try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer
– Agilanbu
Jan 2 at 6:37
try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer
– Agilanbu
Jan 2 at 6:37
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%2f11221491%2fhow-to-make-multiple-line-string-to-single-line-string%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
2
Stupid question.. but why don't you just do this in the first place?:
String str="select * from m_menus;select * from m_roles";Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.– forsvarir
Jun 27 '12 at 7:43
@forsvarir Probably he is getting string in that manner from user input?
– Mukund Samant
Jun 27 '12 at 7:44
I am taking string as input from user
– happy
Jun 27 '12 at 7:53