Android: Divide one String into 2 String values from responseBody - android

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());

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];

how to get String by spliting url in android?

I have a google play store url "https://play.google.com/store/apps/details?id=com.apusapps.launcher&hl=en" from this url I have to store"com.example.launcher" in a seperate variable and compare from package name "com.example.launc" if compare I have to send a json request .how can I do that
Try this:
String s = "https://play.google.com/store/apps/details?id=com.apusapps.launcher&hl=en";
String s2 = s.substring(s.indexOf("=")+1, s.indexOf("&"));
Toast.makeText(this,"Milad: "+s2,Toast.LENGTH_LONG).show();
You can do like this:
String urlString = url.toString();
String finalString = urlString.substring(urlString.indexOf("=")+1,urlString.lastIndexOf("&"));
if(finalString.equals(getApplicationContext().getPackageName())){
//Your code
}

Is there any way to convert JSON String to query String in android?

I am stuck at converting a Json string into query string.
Actually, I want to create query string and from that query string I'll generate a SHA hash and set it in header to send to the server.
Please help!
Is your JSON String currently stored in a JSONObject? If not, this would be the best place to start. Something like this:
JSONObject json = new JSONObject(returnedString);
String firstValue= json.get(firstKey);
String secondValue = json.get(secondKey);
//And then construct your query string using the obtained values
Edit
Generic Method as suggested below...
Something like this:
public String getQueryString(String unparsedString){
StringBuilder sb = new StringBuilder();
JSONObject json = new JSONObject(unparsedString);
Iterator<String> keys = json.keys();
sb.append("?"); //start of query args
while (keys.hasNext()) {
String key = keys.next();
sb.append(key);
sb.append("=");
sb.append(json.get(key);
sb.append("&"); //To allow for another argument.
}
return sb.toString();
If you have more questions regarding JSONObject parsing in Android these docs are very helpful
On Android :
Uri uri=Uri.parse(url_string);
uri.getQueryParameter("para1");
On Android, the Apache libraries provide a Query parser:
http://developer.android.com/reference/org/apache/http/client/utils/URLEncodedUtils.html and http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html

How to split the string a 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.

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]);

Categories

Resources