how to validate cityname,pincode from one string? - android

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

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 do I remove comma from strings for android?

What I have : text1,text2,text3
What I want : text1 text2 text3
replace comma with space?
final String s = "text1,text2,text3".replace(",", " ");
I tried with both replace and replaceAll. But didn't work
This is because both replace() and replaceAll() don't change the String object, they return you a new one. Strings are immutable in Java.
Try This Way:
String data = "text1,text2,text3";
String temp = data.replace(","," ");
Now You have all
This what you should do
String str = "text1,text2,text3"
str = str.replace(","," ");

Add a "," in a string where in it there is this: "}{"

I'm working in a Android app and i need to put a "," between "}{" in my jsonstring before i can work with String#split(). For example i have this:
{"field1":"A","field2":"abcbab","field3":5}
{"field1":"B","field2":"fakfef","field3":25}
{"field1":"A","field2":"faefe","field3":12}
how can i do that?
Try this :
String newJsonStr = yourJsonStr.replaceAll("\\}\\s*\\{", "},{");
String text = "To be or,not,to be, that is the question.";
String k=",{}";
String newText = text.replaceAll(",", k); // Modify the string text
System.out.println(newText);
try this it will solve your problem

Implement Regular expression in android

I got stuck in this regular expression as I am having a string as:
String str = "/abcde/samplename.xyz"
I want to replace this samplename.xyz from a new string, so how can I apply the regular expression in this ?
Try this out.....
String str = "/abcde/samplename.xyz";
String req=str.substring(str.lastIndexOf("/") + 1);
now you get the value of: req=samplename.xyz and you can replace with which ever string value you want
String rep=str.replace(req, "");
String startString=str.substring(0, str.lastIndexOf('/') + 1)
String str = "/abcde/samplename.xyz";
String str1 = str.substring(0, str.lastIndexOf('/') + 1);
Toast.makeText(getApplicationContext(), str1, Toast.LENGTH_LONG).show();
Log.i("strArr[1]=", "" + str1);
check this.i hope its useful to you.
Try this::
String str = "/abcde/samplename.xyz";
String result=str.substring(0, str.lastIndexOf('/') + 1);
Log.d("Hello","Result="+result);
String[] result = str.split("/");
String LastItem = result[str.length -1];
LastItem.replace(newString);

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