Split string into individual words Java
I would like to know how to split up a large string into a series of smaller strings or words.
For example:
I want to walk my dog.
I want to have a string: "I"
,
another string:"want"
, etc.
How would I do this?
java
add a comment |
I would like to know how to split up a large string into a series of smaller strings or words.
For example:
I want to walk my dog.
I want to have a string: "I"
,
another string:"want"
, etc.
How would I do this?
java
2
Please show what you've tried (did you look for the word "split" in the docs for String, for example?)
– Jon Skeet
Jul 30 '12 at 16:53
9
Yes, String#split() is named very ambiguously :-P
– maksimov
Jul 30 '12 at 16:53
add a comment |
I would like to know how to split up a large string into a series of smaller strings or words.
For example:
I want to walk my dog.
I want to have a string: "I"
,
another string:"want"
, etc.
How would I do this?
java
I would like to know how to split up a large string into a series of smaller strings or words.
For example:
I want to walk my dog.
I want to have a string: "I"
,
another string:"want"
, etc.
How would I do this?
java
java
edited May 30 '16 at 20:40
Eric Leschinski
86.4k37318272
86.4k37318272
asked Jul 30 '12 at 16:52
foshofosho
67421020
67421020
2
Please show what you've tried (did you look for the word "split" in the docs for String, for example?)
– Jon Skeet
Jul 30 '12 at 16:53
9
Yes, String#split() is named very ambiguously :-P
– maksimov
Jul 30 '12 at 16:53
add a comment |
2
Please show what you've tried (did you look for the word "split" in the docs for String, for example?)
– Jon Skeet
Jul 30 '12 at 16:53
9
Yes, String#split() is named very ambiguously :-P
– maksimov
Jul 30 '12 at 16:53
2
2
Please show what you've tried (did you look for the word "split" in the docs for String, for example?)
– Jon Skeet
Jul 30 '12 at 16:53
Please show what you've tried (did you look for the word "split" in the docs for String, for example?)
– Jon Skeet
Jul 30 '12 at 16:53
9
9
Yes, String#split() is named very ambiguously :-P
– maksimov
Jul 30 '12 at 16:53
Yes, String#split() is named very ambiguously :-P
– maksimov
Jul 30 '12 at 16:53
add a comment |
9 Answers
9
active
oldest
votes
Use split()
method
Eg:
String s = "I want to walk my dog";
String arr = s.split(" ");
for ( String ss : arr) {
System.out.println(ss);
}
String s already defined?
– fosho
Jul 30 '12 at 16:58
@fosho thanks... it was a typo
– Kumar Vivek Mitra
Jul 30 '12 at 17:00
18
This method will not remove commas, dots, and so on from the words.
– kazy
Mar 27 '15 at 13:20
add a comment |
As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:
String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
String words = s.split("\W+");
The regex means that the delimiters will be anything that is not a word [W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.
5
\W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.
– rghome
May 19 '17 at 13:56
Thank you for pointing that out! Changed answer accordingly.
– Anton Teodor
May 21 '17 at 14:03
add a comment |
A regex can also be used to split words.
w
can be used to match word characters ([A-Za-z0-9_]
), so that punctuation is removed from the results:
String s = "I want to walk my dog, and why not?";
Pattern pattern = Pattern.compile("\w+");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}
Outputs:
I
want
to
walk
my
dog
and
why
not
See Java API documentation for Pattern
Does this produce empty words?
– Joshua Oliphant
Apr 27 '16 at 22:10
add a comment |
See my other answer if your phrase contains accentuated characters :
String listeMots = phrase.split("\P{L}+");
1
This is the best answer.
– rghome
May 19 '17 at 14:06
add a comment |
Yet another method, using StringTokenizer :
String s = "I want to walk my dog";
StringTokenizer tokenizer = new StringTokenizer(s);
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though
– Coding Enthusiast
Jan 20 '17 at 21:42
No, there isn't any array being produced .StringTokenizer
looks for the consecutive tokens in the string and returns them one by one.
– Kao
Jan 21 '17 at 12:55
1
Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
– Tomor
Jan 6 '18 at 19:24
add a comment |
You can use split(" ")
method of the String
class and can get each word as code given below:
String s = "I want to walk my dog";
String strArray=s.split(" ");
for(int i=0; i<strArray.length;i++) {
System.out.println(strArray[i]);
}
add a comment |
Use split()
String words = stringInstance.split(" ");
what must i import?
– fosho
Jul 30 '12 at 16:55
2
nothing<!------------->
– Jigar Joshi
Jul 30 '12 at 16:56
1
Please go through the link from answer
– Jigar Joshi
Jul 30 '12 at 16:59
add a comment |
To include any separators between words (like everything except all lower case and upper case letters), we can do:
String mystring = "hi, there,hi Leo";
String arr = mystring.split("[^a-zA-Z]+");
for(int i = 0; i < arr.length; i += 1)
{
System.out.println(arr[i]);
}
Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z]
, in groups of at least one [+].
add a comment |
you can use Apache commons' StringUtils class
String partsOfString = StringUtils.split("I want to walk my dog",StringUtils.SPACE)
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%2f11726023%2fsplit-string-into-individual-words-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use split()
method
Eg:
String s = "I want to walk my dog";
String arr = s.split(" ");
for ( String ss : arr) {
System.out.println(ss);
}
String s already defined?
– fosho
Jul 30 '12 at 16:58
@fosho thanks... it was a typo
– Kumar Vivek Mitra
Jul 30 '12 at 17:00
18
This method will not remove commas, dots, and so on from the words.
– kazy
Mar 27 '15 at 13:20
add a comment |
Use split()
method
Eg:
String s = "I want to walk my dog";
String arr = s.split(" ");
for ( String ss : arr) {
System.out.println(ss);
}
String s already defined?
– fosho
Jul 30 '12 at 16:58
@fosho thanks... it was a typo
– Kumar Vivek Mitra
Jul 30 '12 at 17:00
18
This method will not remove commas, dots, and so on from the words.
– kazy
Mar 27 '15 at 13:20
add a comment |
Use split()
method
Eg:
String s = "I want to walk my dog";
String arr = s.split(" ");
for ( String ss : arr) {
System.out.println(ss);
}
Use split()
method
Eg:
String s = "I want to walk my dog";
String arr = s.split(" ");
for ( String ss : arr) {
System.out.println(ss);
}
edited Apr 24 '18 at 12:26
Abdullah Khan
5,36122843
5,36122843
answered Jul 30 '12 at 16:54
Kumar Vivek MitraKumar Vivek Mitra
29.3k63663
29.3k63663
String s already defined?
– fosho
Jul 30 '12 at 16:58
@fosho thanks... it was a typo
– Kumar Vivek Mitra
Jul 30 '12 at 17:00
18
This method will not remove commas, dots, and so on from the words.
– kazy
Mar 27 '15 at 13:20
add a comment |
String s already defined?
– fosho
Jul 30 '12 at 16:58
@fosho thanks... it was a typo
– Kumar Vivek Mitra
Jul 30 '12 at 17:00
18
This method will not remove commas, dots, and so on from the words.
– kazy
Mar 27 '15 at 13:20
String s already defined?
– fosho
Jul 30 '12 at 16:58
String s already defined?
– fosho
Jul 30 '12 at 16:58
@fosho thanks... it was a typo
– Kumar Vivek Mitra
Jul 30 '12 at 17:00
@fosho thanks... it was a typo
– Kumar Vivek Mitra
Jul 30 '12 at 17:00
18
18
This method will not remove commas, dots, and so on from the words.
– kazy
Mar 27 '15 at 13:20
This method will not remove commas, dots, and so on from the words.
– kazy
Mar 27 '15 at 13:20
add a comment |
As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:
String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
String words = s.split("\W+");
The regex means that the delimiters will be anything that is not a word [W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.
5
\W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.
– rghome
May 19 '17 at 13:56
Thank you for pointing that out! Changed answer accordingly.
– Anton Teodor
May 21 '17 at 14:03
add a comment |
As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:
String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
String words = s.split("\W+");
The regex means that the delimiters will be anything that is not a word [W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.
5
\W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.
– rghome
May 19 '17 at 13:56
Thank you for pointing that out! Changed answer accordingly.
– Anton Teodor
May 21 '17 at 14:03
add a comment |
As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:
String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
String words = s.split("\W+");
The regex means that the delimiters will be anything that is not a word [W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.
As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:
String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
String words = s.split("\W+");
The regex means that the delimiters will be anything that is not a word [W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.
edited May 21 '17 at 13:50
answered Jul 28 '15 at 11:26
Anton TeodorAnton Teodor
56946
56946
5
\W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.
– rghome
May 19 '17 at 13:56
Thank you for pointing that out! Changed answer accordingly.
– Anton Teodor
May 21 '17 at 14:03
add a comment |
5
\W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.
– rghome
May 19 '17 at 13:56
Thank you for pointing that out! Changed answer accordingly.
– Anton Teodor
May 21 '17 at 14:03
5
5
\W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.
– rghome
May 19 '17 at 13:56
\W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.
– rghome
May 19 '17 at 13:56
Thank you for pointing that out! Changed answer accordingly.
– Anton Teodor
May 21 '17 at 14:03
Thank you for pointing that out! Changed answer accordingly.
– Anton Teodor
May 21 '17 at 14:03
add a comment |
A regex can also be used to split words.
w
can be used to match word characters ([A-Za-z0-9_]
), so that punctuation is removed from the results:
String s = "I want to walk my dog, and why not?";
Pattern pattern = Pattern.compile("\w+");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}
Outputs:
I
want
to
walk
my
dog
and
why
not
See Java API documentation for Pattern
Does this produce empty words?
– Joshua Oliphant
Apr 27 '16 at 22:10
add a comment |
A regex can also be used to split words.
w
can be used to match word characters ([A-Za-z0-9_]
), so that punctuation is removed from the results:
String s = "I want to walk my dog, and why not?";
Pattern pattern = Pattern.compile("\w+");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}
Outputs:
I
want
to
walk
my
dog
and
why
not
See Java API documentation for Pattern
Does this produce empty words?
– Joshua Oliphant
Apr 27 '16 at 22:10
add a comment |
A regex can also be used to split words.
w
can be used to match word characters ([A-Za-z0-9_]
), so that punctuation is removed from the results:
String s = "I want to walk my dog, and why not?";
Pattern pattern = Pattern.compile("\w+");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}
Outputs:
I
want
to
walk
my
dog
and
why
not
See Java API documentation for Pattern
A regex can also be used to split words.
w
can be used to match word characters ([A-Za-z0-9_]
), so that punctuation is removed from the results:
String s = "I want to walk my dog, and why not?";
Pattern pattern = Pattern.compile("\w+");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}
Outputs:
I
want
to
walk
my
dog
and
why
not
See Java API documentation for Pattern
edited Apr 24 '18 at 13:12
Abdullah Khan
5,36122843
5,36122843
answered Jan 31 '15 at 22:12
PetePete
55049
55049
Does this produce empty words?
– Joshua Oliphant
Apr 27 '16 at 22:10
add a comment |
Does this produce empty words?
– Joshua Oliphant
Apr 27 '16 at 22:10
Does this produce empty words?
– Joshua Oliphant
Apr 27 '16 at 22:10
Does this produce empty words?
– Joshua Oliphant
Apr 27 '16 at 22:10
add a comment |
See my other answer if your phrase contains accentuated characters :
String listeMots = phrase.split("\P{L}+");
1
This is the best answer.
– rghome
May 19 '17 at 14:06
add a comment |
See my other answer if your phrase contains accentuated characters :
String listeMots = phrase.split("\P{L}+");
1
This is the best answer.
– rghome
May 19 '17 at 14:06
add a comment |
See my other answer if your phrase contains accentuated characters :
String listeMots = phrase.split("\P{L}+");
See my other answer if your phrase contains accentuated characters :
String listeMots = phrase.split("\P{L}+");
edited May 23 '17 at 12:10
Community♦
11
11
answered Nov 4 '16 at 2:37
Pierre CPierre C
550613
550613
1
This is the best answer.
– rghome
May 19 '17 at 14:06
add a comment |
1
This is the best answer.
– rghome
May 19 '17 at 14:06
1
1
This is the best answer.
– rghome
May 19 '17 at 14:06
This is the best answer.
– rghome
May 19 '17 at 14:06
add a comment |
Yet another method, using StringTokenizer :
String s = "I want to walk my dog";
StringTokenizer tokenizer = new StringTokenizer(s);
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though
– Coding Enthusiast
Jan 20 '17 at 21:42
No, there isn't any array being produced .StringTokenizer
looks for the consecutive tokens in the string and returns them one by one.
– Kao
Jan 21 '17 at 12:55
1
Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
– Tomor
Jan 6 '18 at 19:24
add a comment |
Yet another method, using StringTokenizer :
String s = "I want to walk my dog";
StringTokenizer tokenizer = new StringTokenizer(s);
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though
– Coding Enthusiast
Jan 20 '17 at 21:42
No, there isn't any array being produced .StringTokenizer
looks for the consecutive tokens in the string and returns them one by one.
– Kao
Jan 21 '17 at 12:55
1
Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
– Tomor
Jan 6 '18 at 19:24
add a comment |
Yet another method, using StringTokenizer :
String s = "I want to walk my dog";
StringTokenizer tokenizer = new StringTokenizer(s);
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
Yet another method, using StringTokenizer :
String s = "I want to walk my dog";
StringTokenizer tokenizer = new StringTokenizer(s);
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
answered Sep 10 '14 at 8:57
KaoKao
3,94232857
3,94232857
ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though
– Coding Enthusiast
Jan 20 '17 at 21:42
No, there isn't any array being produced .StringTokenizer
looks for the consecutive tokens in the string and returns them one by one.
– Kao
Jan 21 '17 at 12:55
1
Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
– Tomor
Jan 6 '18 at 19:24
add a comment |
ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though
– Coding Enthusiast
Jan 20 '17 at 21:42
No, there isn't any array being produced .StringTokenizer
looks for the consecutive tokens in the string and returns them one by one.
– Kao
Jan 21 '17 at 12:55
1
Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
– Tomor
Jan 6 '18 at 19:24
ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though
– Coding Enthusiast
Jan 20 '17 at 21:42
ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though
– Coding Enthusiast
Jan 20 '17 at 21:42
No, there isn't any array being produced .
StringTokenizer
looks for the consecutive tokens in the string and returns them one by one.– Kao
Jan 21 '17 at 12:55
No, there isn't any array being produced .
StringTokenizer
looks for the consecutive tokens in the string and returns them one by one.– Kao
Jan 21 '17 at 12:55
1
1
Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
– Tomor
Jan 6 '18 at 19:24
Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
– Tomor
Jan 6 '18 at 19:24
add a comment |
You can use split(" ")
method of the String
class and can get each word as code given below:
String s = "I want to walk my dog";
String strArray=s.split(" ");
for(int i=0; i<strArray.length;i++) {
System.out.println(strArray[i]);
}
add a comment |
You can use split(" ")
method of the String
class and can get each word as code given below:
String s = "I want to walk my dog";
String strArray=s.split(" ");
for(int i=0; i<strArray.length;i++) {
System.out.println(strArray[i]);
}
add a comment |
You can use split(" ")
method of the String
class and can get each word as code given below:
String s = "I want to walk my dog";
String strArray=s.split(" ");
for(int i=0; i<strArray.length;i++) {
System.out.println(strArray[i]);
}
You can use split(" ")
method of the String
class and can get each word as code given below:
String s = "I want to walk my dog";
String strArray=s.split(" ");
for(int i=0; i<strArray.length;i++) {
System.out.println(strArray[i]);
}
edited Jul 19 '14 at 18:18
rwisch45
3,18821733
3,18821733
answered Jul 19 '14 at 17:54
AKTAKT
1334
1334
add a comment |
add a comment |
Use split()
String words = stringInstance.split(" ");
what must i import?
– fosho
Jul 30 '12 at 16:55
2
nothing<!------------->
– Jigar Joshi
Jul 30 '12 at 16:56
1
Please go through the link from answer
– Jigar Joshi
Jul 30 '12 at 16:59
add a comment |
Use split()
String words = stringInstance.split(" ");
what must i import?
– fosho
Jul 30 '12 at 16:55
2
nothing<!------------->
– Jigar Joshi
Jul 30 '12 at 16:56
1
Please go through the link from answer
– Jigar Joshi
Jul 30 '12 at 16:59
add a comment |
Use split()
String words = stringInstance.split(" ");
Use split()
String words = stringInstance.split(" ");
answered Jul 30 '12 at 16:53
Jigar JoshiJigar Joshi
199k35339390
199k35339390
what must i import?
– fosho
Jul 30 '12 at 16:55
2
nothing<!------------->
– Jigar Joshi
Jul 30 '12 at 16:56
1
Please go through the link from answer
– Jigar Joshi
Jul 30 '12 at 16:59
add a comment |
what must i import?
– fosho
Jul 30 '12 at 16:55
2
nothing<!------------->
– Jigar Joshi
Jul 30 '12 at 16:56
1
Please go through the link from answer
– Jigar Joshi
Jul 30 '12 at 16:59
what must i import?
– fosho
Jul 30 '12 at 16:55
what must i import?
– fosho
Jul 30 '12 at 16:55
2
2
nothing<!------------->
– Jigar Joshi
Jul 30 '12 at 16:56
nothing<!------------->
– Jigar Joshi
Jul 30 '12 at 16:56
1
1
Please go through the link from answer
– Jigar Joshi
Jul 30 '12 at 16:59
Please go through the link from answer
– Jigar Joshi
Jul 30 '12 at 16:59
add a comment |
To include any separators between words (like everything except all lower case and upper case letters), we can do:
String mystring = "hi, there,hi Leo";
String arr = mystring.split("[^a-zA-Z]+");
for(int i = 0; i < arr.length; i += 1)
{
System.out.println(arr[i]);
}
Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z]
, in groups of at least one [+].
add a comment |
To include any separators between words (like everything except all lower case and upper case letters), we can do:
String mystring = "hi, there,hi Leo";
String arr = mystring.split("[^a-zA-Z]+");
for(int i = 0; i < arr.length; i += 1)
{
System.out.println(arr[i]);
}
Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z]
, in groups of at least one [+].
add a comment |
To include any separators between words (like everything except all lower case and upper case letters), we can do:
String mystring = "hi, there,hi Leo";
String arr = mystring.split("[^a-zA-Z]+");
for(int i = 0; i < arr.length; i += 1)
{
System.out.println(arr[i]);
}
Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z]
, in groups of at least one [+].
To include any separators between words (like everything except all lower case and upper case letters), we can do:
String mystring = "hi, there,hi Leo";
String arr = mystring.split("[^a-zA-Z]+");
for(int i = 0; i < arr.length; i += 1)
{
System.out.println(arr[i]);
}
Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z]
, in groups of at least one [+].
edited Nov 20 '16 at 6:15
Danh
4,96672335
4,96672335
answered Nov 20 '16 at 5:56
Akib ImtiazAkib Imtiaz
233
233
add a comment |
add a comment |
you can use Apache commons' StringUtils class
String partsOfString = StringUtils.split("I want to walk my dog",StringUtils.SPACE)
add a comment |
you can use Apache commons' StringUtils class
String partsOfString = StringUtils.split("I want to walk my dog",StringUtils.SPACE)
add a comment |
you can use Apache commons' StringUtils class
String partsOfString = StringUtils.split("I want to walk my dog",StringUtils.SPACE)
you can use Apache commons' StringUtils class
String partsOfString = StringUtils.split("I want to walk my dog",StringUtils.SPACE)
answered Apr 24 '18 at 12:32
Gagan ChouhanGagan Chouhan
925
925
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%2f11726023%2fsplit-string-into-individual-words-java%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
Please show what you've tried (did you look for the word "split" in the docs for String, for example?)
– Jon Skeet
Jul 30 '12 at 16:53
9
Yes, String#split() is named very ambiguously :-P
– maksimov
Jul 30 '12 at 16:53