Android Split string
String currentString = "Fruit: they taste good";
String[] separated = currentString.split(":");
separated[0]; // this will contain "Fruit"
separated[1]; // this will contain " they taste good"
You may want to remove the space to the second String:
separated[1] = separated[1].trim();
If you want to split the string with a special character like dot(.) you should use escape character \ before the dot
Example:
String currentString = "Fruit: they taste good.very nice actually";
String[] separated = currentString.split("\\.");
separated[0]; // this will contain "Fruit: they taste good"
separated[1]; // this will contain "very nice actually"
There are other ways to do it. For instance, you can use the StringTokenizer
class (from java.util
):
StringTokenizer tokens = new StringTokenizer(currentString, ":");
String first = tokens.nextToken();// this will contain "Fruit"
String second = tokens.nextToken();// this will contain " they taste good"
// in the case above I assumed the string has always that syntax (foo: bar)
// but you may want to check if there are tokens or not using the hasMoreTokens method
split string android studio
You can just use the array returned by String#split
instead of using lots of variables.
String[] ans=anskey.split("");
Instead of using ans1
, you can then just use ans[0]
.
However, if you want to split for characters, you can also do it like this so you get a char[]
:
char[] ans=anskey.toCharArray();
This approach is way more robust as split
uses a regex.
And if you nedd to get the values of all variables, you can just iterate over them:
for(int i=0;i<ans.length;i++){
//Do something with ans[i]
}
or
for(String distinctAns:ans){
//Do something with ans[i]
}
Split a string in android
Java version
String s = "MAT:TO:My address email;SUB:My title;BODY:My content;;";
String[] arrayString = s.split(";");
String email = arrayString[0];
String title = arrayString[1];
String body = arrayString[2];
email= email.substring(email.indexOf("MAT:TO:") + 7, email.length());
title= title.substring(title.indexOf("SUB:") + 4, title.length());
body= body.substring(body.indexOf("BODY:") + 5, body.length());
Especially Android official is used kotlin version:
val s = "MAT:TO:My address email;SUB:My title;BODY:My content;;"
val arrayString = s.split(";").toTypedArray()
var email = arrayString[0]
var title = arrayString[1]
var body = arrayString[2]
email = email.substring(email.indexOf("MAT:TO:") + 7, email.length)
title = title.substring(title.indexOf("SUB:") + 4, title.length)
body = body.substring(body.indexOf("BODY:") + 5, body.length)
Split String with .(dot) character java android
You need to escape .
using \
Eg:
String s="Android_a_b.pdf";
String[] parts = s.split("\\."); // escape .
String part1 = parts[0];
String part2 = parts[1];
Now it will split by .
Split(regex) in Java
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with
the given expression and a limit argument of zero. Trailing empty
strings are therefore not included in the resulting array.
Keep in mind that
Parameters:
regex - the delimiting regular expression
Java(android): How to I split a string by every two line breaks?
First of all you should escape the \
with another \
like this:
string.split("\\n\\n");
Another way is using system default line separator:
string.split(System.getProperty("line.separator")+"{2}");
or you can try mix this:
string.split("(\\r\\n|"+System.getProperty("line.separator")+")+");
split
need RegExp, so you can try variants for your problem.
And don't forget that sometimes new line is not only \n
symbol, for Windows files it can be \r\n
char sequence.
Related Topics
Sending a JSON Http Post Request from Android
How to Use Vectordrawable in Buttons and Textviews Using Android:Drawableright
Inputstream.Available() Is 0 Always
How to Add a Shortcut Key for a Jbutton in Java
Lambdas: Local Variables Need Final, Instance Variables Don'T
Calling One Jframe from Another Using Timer Without Any Buttons
How to Set a Jvm Timezone Properly
How to Use Xpath on Xml Docs Having Default Namespace
Gradle Does Not Find Tools.Jar
How to Read Jsch Command Output
How to Get the Current Screen Orientation
Namenode: Java.Net.Bindexception
Spring @Transactional - Isolation, Propagation
How to Handle Windows File Upload Using Selenium Webdriver
Mousemotionlistener in Java Swing, Using It with Components Inside Components etc
How to Read the Value of a Annotation in Java
Parsing a String to Date Format in Java Defaults Date to 1 and Month to January