This question already has answers here:
Java: String split(): I want it to include the empty strings at the end [duplicate]
(3 answers)
Closed 6 years ago.
I need split a string using ';' as separator, if the string has all of fields filled it's works good, but if some fields are not filled, like
string.split("A;B;C;;;") not work... for this string I expected that output would
[0]=A
[1]=B
[2]=C
[3]=''
[4]=''
[5]=''
, but the output is only first three fields
[0]=A
[1]=B
[3]=C
... the other fields wasn't created
Some clue how to solve this?
The ; character seperates C from the end of the string, no matter how many of them there are. The String.split() method will not return plain white space or an empty string.
If not mistaken, split looks for characters[ASCII] between the two separators and in case of
str.split("A;B;C;;;"),
there are no characters between the two semi colons. Split by defaults remove the empty string, to overrule that we need to use the overloaded split as detailed here in Java docs.
Try this if possible based on your input architecture:
String str = "A;B;C;;;";
str.split(";", -1);
This helps to look even for null string output would be
[0] = "A"
[1] = "B"
[2] = "C"
[3] = ""
[4] = ""
Hope this helps.
Related
This question already has an answer here:
Extract first two characters of a String in Java [closed]
(1 answer)
Closed 1 year ago.
I've a string with complete language name and I wanted to set first two characters of that language name to my text view. Like I've a language name AFRIKAANS and I only wanted to set AF on my text view. How can I do this
String string = "AFRIKAANS";
yourTextView.setText(string.substring(0, 2));
you can use substring method like this
"AFRIKAANS".substring(0,2)
This question already has answers here:
How to remove leading and trailing whitespace from the string in Java?
(11 answers)
Strip Leading and Trailing Spaces From Java String
(8 answers)
Closed 2 years ago.
Assuming I have a String like this one in Android:
" This is an example String "
and i want to make it become:
"This is an example String"
I tried to use the split() but is not found in Android, is there any other method i'm not getting?
Thx
Store it in a string like this
String str = " This is an example String ";
then use the trim() method :
str.trim();
that's it!
The method automatically removes spaces in front and back
This question already has answers here:
Android split not working correctly
(2 answers)
Closed 7 years ago.
in my android app, i would like to split an array value into another array.
i have an Array with the name ArrayA.
log of ArrayA[0]:
Peter|Mustermann
now i would like to split Peter and Mustermann, i try this:
String [] ArrayB = ArrayA[0].split("|");
But the Log of ArrayB[0] and ArrayB[1] will not be:
Peter
And
Mustermann
it will be:
P
And
nothing
Any ideas ? :(
The public String[] split(String regex) works with a regular expression.
In a regular expression | is a reserved character, so try to escape it using
String [] ArrayB = ArrayA[0].split("\\|");
See here for more information about other reserved characters: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum
And see here for a compilable sample: http://ideone.com/fjXNoJ
You'll have use it as follows:
String [] ArrayB = Array[0].trim().split("\\|");
As '\' is a special character also (along with |), two backslashes in a string literal will mean one backslash in the actual String, which in turn will represent a regular expression that matches a single backslash character.
Just use a single quote
String [] ArrayB = ArrayA[0].split('|');
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 8 years ago.
Hi how can i return Grapes from the string below, i want to search through a string and return a text in the middle of the string after four character and discard the rest of the text.
String grapes = "2 x Grapes #Walmart";
Thanks for helping me guys the code below worked
String grapes = "2 x Grapes #Walmart";
String[] split = grapes.split("\\s+");
String fsplit = split[2];
my suggestion will be not to use a regex for this . But just in case you find no other way round, use this :
(\w+\s){3}
you will get the third word in the first backreference. \1 or $1 whichever supports your compiler
demo here : http://regex101.com/r/jB5nN0
This may help you:
^[\\d]+\\sx\\s(.*?)\\s+.*?$
Explanation:
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single digit 0..9 «[\d]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s»
Match the character “x” literally «x»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s»
Match the regular expression below and capture its match into backreference number 1 «(.*?)»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
i have a String displayed on a WebView as "Siwy & Para Wino"
i fetch it from url , i got a string "Siwy%2B%2526%2BPara%2BWino". // be corrected
now i'm trying to use URLDecoder to solve this problem :
String decoded_result = URLDecoder.decode(url); // the url is "Siwy+%26+Para+Wino"
then i print it out , i still saw "Siwy+%26+Para+Wino"
Could anyone tell me why?
From the documentation (of URLDecoder):
This class is used to decode a string which is encoded in the application/x-www-form-urlencoded MIME content type.
We can look at the specification to see what a form-urlencoded MIME type is:
The form field names and values are escaped: space characters are replaced by '+', and then reserved characters are escaped as per [URL]; that is, non-alphanumeric characters are replaced by '%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks, as in multi-line text field values, are represented as CR LF pairs, i.e. '%0D%0A'.
Since the specification calls for a percent sign followed by two hexadecimal digits for the ASCII code, the first time you call the decode(String s) method, it converts those into single characters, leaving the two additional characters 26 intact. The value %25 translates to % so the result after the first decoding is %26. Running decode one more time simply translates %26 back into &.
String decoded_result = URLDecoder.decode(URLDecoder.decode(url));
You can also use the Uri class if you have UTF-8-encoded strings:
Decodes '%'-escaped octets in the given string using the UTF-8 scheme.
Then use:
String decoded_result = Uri.decode(Uri.decode(url));
thanks for all answers , i solved it finally......
solution:
after i used URLDecoder.decode twice (oh my god) , i got what i want.
String temp = URLDecoder.decode( url); // url = "Siwy%2B%2526%2BPara%2BWino"
String result = URLDecoder.decode( temp ); // temp = "Siwy+%26+Para+Wino"
// result = "Swy & Para Wino". !!! oh good job.
but i still don't know why.. could someone tell me?