Removing spaces from string - android

I'm trying to remove all the spaces from a string derived from user input, but for some reason it isn't working for me. Here is my code.
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonGo:
String input = EditTextinput.getText().toString();
input = String.replace(" ", "");
url = ur + input + l;
Intent myIntent = new Intent(start.this, Main.class);
myIntent.putExtra("URL", url);
start.this.startActivity(myIntent);
break;
}
}

String input = EditTextinput.getText().toString();
input = input.replace(" ", "");
Sometimes you would want to remove only the spaces at the beginning or end of the String (not the ones in the middle). If that's the case you can use trim:
input = input.trim();

When I am reading numbers from contact book, then it doesn't worked
I used
number=number.replaceAll("\\s+", "");
It worked and for url you may use
url=url.replaceAll(" ", "%20");

I also had this problem. To sort out the problem of spaces in the middle of the string this line of code always works:
String field = field.replaceAll("\\s+", "");

Try this:
String urle = HOST + url + value;
Then return the values from:
urle.replace(" ", "%20").trim();

String res =" Application "
res=res.trim();
o/p: Application
Note: White space ,blank space are trim or removed

Using kotlin you can write like:
val resultStr = yourString.replace(" ", "")
Example:
val yourString = "Android Kotlin"
val resultStr = yourString.replace(" ", "")
Result: AndroidKotlin

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 to get the searched text in a string?

I want to get a text that it is a part of an string.For example: I have a string like "I am Vahid" and I want to get everything that it's after "am".
The result will be "Vahid"
How can I do it?
Try:
Example1
String[] separated = CurrentString.split("am");
separated[0]; // I am
separated[1]; // Vahid
Example2
StringTokenizer tokens = new StringTokenizer(CurrentString, "am");
String first = tokens.nextToken();// I am
String second = tokens.nextToken(); //Vahid
Try:
String text = "I am Vahid";
String after = "am";
int index = text.indexOf(after);
String result = "";
if(index != -1){
result = text.substring(index + after.length());
}
System.out.print(result);
Just use like this, call the method with your string.
public String trimString(String stringyouwanttoTrim)
{
if(stringyouwanttoTrim.contains("I am")
{
return stringyouwanttoTrim.split("I am")[1].trim();
}
else
{
return stringyouwanttoTrim;
}
}
If you prefer to split your sentence by blank space you could do like this :
String [] myStringElements = "I am Vahid".split(" ");
System.out.println("your name is " + myStringElements[myStringElements.length - 1]);

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

If statement not returning string

Hello i have small problem with android studio.
I have made simple code that checks if user has inserted string with http:// if not then add http:// to string.
Here is part of my code:
if (!host.contains("http://")) {
String playlistUrl = "http://" + host + "/test.m3u";
}else{
String playlistUrl = host + "/test.m3u";
}
intent.setData(Uri.parse(playlistUrl));
Android Studio drops me an error on last line (cannot resolve symbol playlistUrl)
intent.setData(Uri.parse(playlistUrl));
But why? If statement should return string playlistUrl....
You're creating the variable inside of the if ... else statement - it means it won't be visible outside.
You can implement it like this:
String playlistUrl;
if (...) {
playlistUrl = ...;
}
else {
playlistUrl = ...;
}
Declare playlistUrl outside the if-else construct:
String playlistUrl;
if (!host.contains("http://")) {
playlistUrl = "http://" + host + "/test.m3u";
} else{
playlistUrl = host + "/test.m3u";
}
intent.setData(Uri.parse(playlistUrl));
you need to do it this way your not using the same variable by redeclaring it
String playlistUrl = "";
if (!host.contains("http://")) {
"http://" + host + "/test.m3u";
}else{
playlistUrl = host + "/test.m3u";
}
intent.setData(Uri.parse(playlistUrl));
You have to declare and instantiate your var playListUrl outside the if statement. The reason for that is that the compiler only translates to bytecode without evaluating expressions.

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

Categories

Resources