How to split the string a android - android

I am working on android application. I am getting the String value from webservice extension
i want to split 4 from the string by using index .pls tell me how can do this
String version = "1.4.2";

Try this..
String version = "1.4.2";
Log.v("value ",""+version.split("\\.")[1]);
For more information Refer Link1,Link2

can you try this :
String[] items = "1.4.2".split("\\.");
String version = items[1].toString();

have all the sub-part of your string using
String[] strs = version.split("\.");
now if you want 4 from (1.4.2), do following:
String mMiddle = strs[1]; // it will give 4 as a string in mMiddle
if you want every sub-part:
String mStart = strs[0]; //returns 1
String mMiddle = strs[1]; //returns 4
String mLast = strs[2]; //returns 2

Try this code:
String version = "1.4.2";
String arr[]=version.split("\\.");
String value=arr[1].toString();
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
thank you.

Related

How to Split a string by Comma And Received them Inside Different EditText in Android?

I have Two EditText(id=et_tnum,et_pass). I Received a String like 12345,mari#123 inside EditText1(et_tnum) . I want to Split them by Comma and After Comma i should Receive Remainder string into EditText2(et_pass). Here 12345,mari#123 is Account Number & Password Respectively.
String[] strSplit = YourString.split(",");
String str1 = strSplit[0];
String str2 = strSplit[1];
EditText1.setText(str1);
EditText2.setText(str2);
String CurrentString = "12345,mari#123";
String[] separated = CurrentString.split(",");
//If this Doesn't work please try as below
//String[] separated = CurrentString.split("\\,");
separated[0]; // this will contain "12345"
separated[1]; // this will contain "mari#123"
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 "12345"
String second = tokens.nextToken();// this will contain "mari#123"
// 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
This answer is from this post
You can use
String[] strArr = yourString.split("\\,");
et_tnum.setText(strArr[0]);
et_pass.setText(strArr[1]);
Try
String[] data = str.split(",");
accountNumber = data[0];
password = data[1];

ArrayIndexOutOfBoundsException at splitting string

In my Android app I have a string which value is always from type yo_2014_rojo.
I need to split the string in three parts: part1 ="yo" part2="2014" and part3="rojo".
I am trying to do it as follows:
String s[] = dato_seleccionado.split("_");
String s1 = s[0];
String s2 = s[1];
String s3 = s[2];
but the app crashes with an exception: ArrayIndexOutOfBoundsException.
Any help is welcome.
Try this...
String str = "yo_2014_rojo";
StringTokenizer token = new StringTokenizer(str , "_");
String part1 = token.nextToken(); //yo
String part2 = token.nextToken(); //2014
String part3 = token.nextToken(); //rojo
stringname.split() takes Perl regex as an argument.
Try escaping the underscore, like this:
String[] spliced = dato_seleccionado.split("\\_");
Try doing it like this:
String[] spliced = dato_seleccionado.split("_");
System.out.println(Arrays.toString(spliced)); // check if you have the correct output
String s1 = spliced[0];
String s2 = spliced[1];
String s3 = spliced[2];

how to validate cityname,pincode from one string?

Example: I have a EditText and I want to check the first word is the city name and second word is the pincode. These both words are separated by comma(,).
Hey try this if you don't want to use split. YOu need to get string into a variable from edittext and then use the following code for doing yourself able to validate :)
String str = "tim,52250";
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Do this way..
String content="Mehsana,384001";
String[] contentArray=content.split(",");
And you will get
contentArray[0]=Mehsana
contentArray[1]=384001
then you can validate each string content..
Use split() to get the things done.
Ex:
String s= "abc,123"
String s1[]=s.split(",");
String city=s1[0];
String pincode=s1[1];
Try this
String strInput = editText.getText().toString();
String strSplit [] = strInput.split(",");
System.out.println("CityName : " + strSplit[0]);
System.out.println("PinCode : " + strSplit[1]);
String data = "ali,524513"
String []array = data.split(",")
you can validate array[0] and array[1] :)
System.out.println("Name: "+array[0]+" code: "+array[1]);

Read string from a combined string Help how to read this [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 9 years ago.
i want to read this string
String "abc;def;ghi;jklm;nopqr"
i want to get these diffrent diffrent
string a = abc;
string b = def;
string c = ghi;
string f = nopqr;`
how i read this , plz help me,
You can use the split method:
String[] tokens = str.split(";")
for example:
String myString = "abc;def;ghi;jklm;nopqr";
String[] tokens = myString.split(";")
//tokens[0]="abc"
//tokens[1]="def"
//...
Try this:
String string = "aaa;bbb";
String[] parts = string.split(";");
String part1 = parts[0]; //aaa
String part2 = parts[1];//bbb

Android: Divide one String into 2 String values from responseBody

With this function I got a String from the server as a response:
String responseBody = EntityUtils.toString(response.getEntity());
the String value I retrieve looks more or less like this:
"http://url.com 765889"
I want to divide the URL and the numbers into 2 String values, it should be:
String 1="http://url.com"
String 2="765889"
How am I able to perform that?
Use the split() function:
String[] parts = responseBody.split(" ");
Maybe like this
String 1 = responseBody.substring(0,responseBody.indexOf(" ")-1);
String 2 = responseBody.substring(responseBody.indexOf(" ").responseBody.length()-1);
Simples do this
StringTokenizer t = new StringTokenizer("http://url.com 765889"," ");
Log.d("first", t.nextToken());
Log.d("second", t.nextToken());

Categories

Resources