Find and Replace string in Android - android

myString = "my name=\"ppshein\""
How can I remove "=\"ppshein\"" from the above string in Android?

You can remove strings this way:
string.replace("=\"ppshein\"", "");

myString.replace("=\"ppshein\"", "");

Assuming you have a string called myString and you want a new string with all occurrences of ="ppshein" removed, try
String newString = myString.replace("=\"ppshein\"", "")

First of all, the above string won't compile. you need to write it like: myString = "my name=\"ppshein\""
Secondly, use: myString = myString.replace("=\"ppshein\"", "");

myString = myString.replaceAll("StringToReplace", "");

Related

String elimination with exception?

String first = "books(32/400)";
String second = first.replaceAll("\\D+", "");
Result is second = 32400. That's OK.
But, I want to this result:
second = 400
If you have fixed structure with slash and bracket try tis
var result=first.substring(first.lastIndexOf("/")+1,first.lastIndexOf(")"));
result=400
Try this regex, you can tweak the pattern to exclude what you do not need.
Regex regEx = new Regex(#"[books()]");
string output = Regex.Replace(regEx.Replace(#"books(32/400)", ""), #"\s+", "");
string result = output.Split('/').Last();
Try it with:
String second = first.substring(first.indexOf("/") + 1, first.indexOf(")"));
use this pattern : "\\d+"
String first = "books(32/400)";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(first);
while (m.find()) {
System.out.println(m.group());
}

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

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