Case insensitive String split() method
When I perform
String test="23x34 ";
String array=test.split("x"); //splitting using simple letter
I got two items in array as 23 and 34
but when I did
String test="23x34 ";
String array=test.split("X"); //splitting using capitalletter
I got one item in array 23x34
So is there any way I can use the split method as case insensitive or whether there is any other method that can help?
java string split
add a comment |
When I perform
String test="23x34 ";
String array=test.split("x"); //splitting using simple letter
I got two items in array as 23 and 34
but when I did
String test="23x34 ";
String array=test.split("X"); //splitting using capitalletter
I got one item in array 23x34
So is there any way I can use the split method as case insensitive or whether there is any other method that can help?
java string split
add a comment |
When I perform
String test="23x34 ";
String array=test.split("x"); //splitting using simple letter
I got two items in array as 23 and 34
but when I did
String test="23x34 ";
String array=test.split("X"); //splitting using capitalletter
I got one item in array 23x34
So is there any way I can use the split method as case insensitive or whether there is any other method that can help?
java string split
When I perform
String test="23x34 ";
String array=test.split("x"); //splitting using simple letter
I got two items in array as 23 and 34
but when I did
String test="23x34 ";
String array=test.split("X"); //splitting using capitalletter
I got one item in array 23x34
So is there any way I can use the split method as case insensitive or whether there is any other method that can help?
java string split
java string split
asked May 16 '13 at 8:00
Sanjaya LiyanageSanjaya Liyanage
3,37172648
3,37172648
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
Use regex pattern [xX] in split
String x = "24X45";
String res = x.split("[xX]");
System.out.println(Arrays.toString(res));
thanks it works.
– Sanjaya Liyanage
May 16 '13 at 8:18
add a comment |
split uses, as the documentation suggests, a regexp. a regexp for your example would be :
"[xX]"
Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :
"(?i)x"
In this case, x can be any litteral properly escaped.
downvoter please comment.
– njzk2
May 16 '13 at 9:35
3
+1 for(?i), it can be used for sentences. Ex:String array = "24xXx45".split("(?i)XXX"); // [24, 45]
– Fernando Leal
Mar 28 '14 at 18:42
1
If you have unicode chars you should rather use (?iu) flag instead.
– NikolaB
Oct 14 '15 at 12:06
1
"24x45".split("(?i)X")not working in chrome 67
– Chan Tzish
Jun 18 '18 at 23:32
3
@ChanTzish this question is about Java
– njzk2
Jun 19 '18 at 3:06
|
show 1 more comment
You can also use an embedded flag in your regex:
String array = test.split("(?i)x"); // splits case insensitive
add a comment |
I personally prefer using
String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
String parts = modified.split(splitterValue);
In this way you can ensure any regex will work, as long as you have a unique splitter value
add a comment |
Java's String class' split method also accepts regex.
To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split
1
"also accepts regex" actually only accepts regex.
– wchargin
Jun 4 '13 at 5:15
@WChargin So true. And I realized it just yesterday.
– zEro
Jun 6 '13 at 14:11
add a comment |
You could use a regex as an argument to split, like this:
"32x23".split("[xX]");
Or you could use a StringTokenizer that lets you set its set of delimiters, like this:
StringTokenizer st = new StringTokenizer("32x23","xX");
// ^^ ^^
// string delimiter
This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.
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%2f16581977%2fcase-insensitive-string-split-method%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
Use regex pattern [xX] in split
String x = "24X45";
String res = x.split("[xX]");
System.out.println(Arrays.toString(res));
thanks it works.
– Sanjaya Liyanage
May 16 '13 at 8:18
add a comment |
Use regex pattern [xX] in split
String x = "24X45";
String res = x.split("[xX]");
System.out.println(Arrays.toString(res));
thanks it works.
– Sanjaya Liyanage
May 16 '13 at 8:18
add a comment |
Use regex pattern [xX] in split
String x = "24X45";
String res = x.split("[xX]");
System.out.println(Arrays.toString(res));
Use regex pattern [xX] in split
String x = "24X45";
String res = x.split("[xX]");
System.out.println(Arrays.toString(res));
answered May 16 '13 at 8:01
harshharsh
5,96412230
5,96412230
thanks it works.
– Sanjaya Liyanage
May 16 '13 at 8:18
add a comment |
thanks it works.
– Sanjaya Liyanage
May 16 '13 at 8:18
thanks it works.
– Sanjaya Liyanage
May 16 '13 at 8:18
thanks it works.
– Sanjaya Liyanage
May 16 '13 at 8:18
add a comment |
split uses, as the documentation suggests, a regexp. a regexp for your example would be :
"[xX]"
Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :
"(?i)x"
In this case, x can be any litteral properly escaped.
downvoter please comment.
– njzk2
May 16 '13 at 9:35
3
+1 for(?i), it can be used for sentences. Ex:String array = "24xXx45".split("(?i)XXX"); // [24, 45]
– Fernando Leal
Mar 28 '14 at 18:42
1
If you have unicode chars you should rather use (?iu) flag instead.
– NikolaB
Oct 14 '15 at 12:06
1
"24x45".split("(?i)X")not working in chrome 67
– Chan Tzish
Jun 18 '18 at 23:32
3
@ChanTzish this question is about Java
– njzk2
Jun 19 '18 at 3:06
|
show 1 more comment
split uses, as the documentation suggests, a regexp. a regexp for your example would be :
"[xX]"
Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :
"(?i)x"
In this case, x can be any litteral properly escaped.
downvoter please comment.
– njzk2
May 16 '13 at 9:35
3
+1 for(?i), it can be used for sentences. Ex:String array = "24xXx45".split("(?i)XXX"); // [24, 45]
– Fernando Leal
Mar 28 '14 at 18:42
1
If you have unicode chars you should rather use (?iu) flag instead.
– NikolaB
Oct 14 '15 at 12:06
1
"24x45".split("(?i)X")not working in chrome 67
– Chan Tzish
Jun 18 '18 at 23:32
3
@ChanTzish this question is about Java
– njzk2
Jun 19 '18 at 3:06
|
show 1 more comment
split uses, as the documentation suggests, a regexp. a regexp for your example would be :
"[xX]"
Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :
"(?i)x"
In this case, x can be any litteral properly escaped.
split uses, as the documentation suggests, a regexp. a regexp for your example would be :
"[xX]"
Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :
"(?i)x"
In this case, x can be any litteral properly escaped.
answered May 16 '13 at 8:07
njzk2njzk2
32.8k44991
32.8k44991
downvoter please comment.
– njzk2
May 16 '13 at 9:35
3
+1 for(?i), it can be used for sentences. Ex:String array = "24xXx45".split("(?i)XXX"); // [24, 45]
– Fernando Leal
Mar 28 '14 at 18:42
1
If you have unicode chars you should rather use (?iu) flag instead.
– NikolaB
Oct 14 '15 at 12:06
1
"24x45".split("(?i)X")not working in chrome 67
– Chan Tzish
Jun 18 '18 at 23:32
3
@ChanTzish this question is about Java
– njzk2
Jun 19 '18 at 3:06
|
show 1 more comment
downvoter please comment.
– njzk2
May 16 '13 at 9:35
3
+1 for(?i), it can be used for sentences. Ex:String array = "24xXx45".split("(?i)XXX"); // [24, 45]
– Fernando Leal
Mar 28 '14 at 18:42
1
If you have unicode chars you should rather use (?iu) flag instead.
– NikolaB
Oct 14 '15 at 12:06
1
"24x45".split("(?i)X")not working in chrome 67
– Chan Tzish
Jun 18 '18 at 23:32
3
@ChanTzish this question is about Java
– njzk2
Jun 19 '18 at 3:06
downvoter please comment.
– njzk2
May 16 '13 at 9:35
downvoter please comment.
– njzk2
May 16 '13 at 9:35
3
3
+1 for
(?i), it can be used for sentences. Ex: String array = "24xXx45".split("(?i)XXX"); // [24, 45]– Fernando Leal
Mar 28 '14 at 18:42
+1 for
(?i), it can be used for sentences. Ex: String array = "24xXx45".split("(?i)XXX"); // [24, 45]– Fernando Leal
Mar 28 '14 at 18:42
1
1
If you have unicode chars you should rather use (?iu) flag instead.
– NikolaB
Oct 14 '15 at 12:06
If you have unicode chars you should rather use (?iu) flag instead.
– NikolaB
Oct 14 '15 at 12:06
1
1
"24x45".split("(?i)X") not working in chrome 67– Chan Tzish
Jun 18 '18 at 23:32
"24x45".split("(?i)X") not working in chrome 67– Chan Tzish
Jun 18 '18 at 23:32
3
3
@ChanTzish this question is about Java
– njzk2
Jun 19 '18 at 3:06
@ChanTzish this question is about Java
– njzk2
Jun 19 '18 at 3:06
|
show 1 more comment
You can also use an embedded flag in your regex:
String array = test.split("(?i)x"); // splits case insensitive
add a comment |
You can also use an embedded flag in your regex:
String array = test.split("(?i)x"); // splits case insensitive
add a comment |
You can also use an embedded flag in your regex:
String array = test.split("(?i)x"); // splits case insensitive
You can also use an embedded flag in your regex:
String array = test.split("(?i)x"); // splits case insensitive
answered May 16 '13 at 8:08
jlordojlordo
31.3k64371
31.3k64371
add a comment |
add a comment |
I personally prefer using
String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
String parts = modified.split(splitterValue);
In this way you can ensure any regex will work, as long as you have a unique splitter value
add a comment |
I personally prefer using
String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
String parts = modified.split(splitterValue);
In this way you can ensure any regex will work, as long as you have a unique splitter value
add a comment |
I personally prefer using
String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
String parts = modified.split(splitterValue);
In this way you can ensure any regex will work, as long as you have a unique splitter value
I personally prefer using
String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
String parts = modified.split(splitterValue);
In this way you can ensure any regex will work, as long as you have a unique splitter value
answered Dec 23 '14 at 23:29
user2981810user2981810
149210
149210
add a comment |
add a comment |
Java's String class' split method also accepts regex.
To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split
1
"also accepts regex" actually only accepts regex.
– wchargin
Jun 4 '13 at 5:15
@WChargin So true. And I realized it just yesterday.
– zEro
Jun 6 '13 at 14:11
add a comment |
Java's String class' split method also accepts regex.
To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split
1
"also accepts regex" actually only accepts regex.
– wchargin
Jun 4 '13 at 5:15
@WChargin So true. And I realized it just yesterday.
– zEro
Jun 6 '13 at 14:11
add a comment |
Java's String class' split method also accepts regex.
To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split
Java's String class' split method also accepts regex.
To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split
answered May 16 '13 at 8:04
zErozEro
1,1281223
1,1281223
1
"also accepts regex" actually only accepts regex.
– wchargin
Jun 4 '13 at 5:15
@WChargin So true. And I realized it just yesterday.
– zEro
Jun 6 '13 at 14:11
add a comment |
1
"also accepts regex" actually only accepts regex.
– wchargin
Jun 4 '13 at 5:15
@WChargin So true. And I realized it just yesterday.
– zEro
Jun 6 '13 at 14:11
1
1
"also accepts regex" actually only accepts regex.
– wchargin
Jun 4 '13 at 5:15
"also accepts regex" actually only accepts regex.
– wchargin
Jun 4 '13 at 5:15
@WChargin So true. And I realized it just yesterday.
– zEro
Jun 6 '13 at 14:11
@WChargin So true. And I realized it just yesterday.
– zEro
Jun 6 '13 at 14:11
add a comment |
You could use a regex as an argument to split, like this:
"32x23".split("[xX]");
Or you could use a StringTokenizer that lets you set its set of delimiters, like this:
StringTokenizer st = new StringTokenizer("32x23","xX");
// ^^ ^^
// string delimiter
This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.
add a comment |
You could use a regex as an argument to split, like this:
"32x23".split("[xX]");
Or you could use a StringTokenizer that lets you set its set of delimiters, like this:
StringTokenizer st = new StringTokenizer("32x23","xX");
// ^^ ^^
// string delimiter
This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.
add a comment |
You could use a regex as an argument to split, like this:
"32x23".split("[xX]");
Or you could use a StringTokenizer that lets you set its set of delimiters, like this:
StringTokenizer st = new StringTokenizer("32x23","xX");
// ^^ ^^
// string delimiter
This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.
You could use a regex as an argument to split, like this:
"32x23".split("[xX]");
Or you could use a StringTokenizer that lets you set its set of delimiters, like this:
StringTokenizer st = new StringTokenizer("32x23","xX");
// ^^ ^^
// string delimiter
This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.
answered May 16 '13 at 8:13
ananaanana
1,251811
1,251811
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%2f16581977%2fcase-insensitive-string-split-method%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