How can I check if a string contains anything other than whitespace?
This code didn't work:
String string = " \n\n\t\t ";
if(string.length()==0) doSomething();
since spaces and new lines have values.
Can anyone tell me how can I do it?
Note: minSDKVersion = 5
Regards :)
Try this:
if (string.trim().length() == 0) { /* all white space */ }
Alternatively, you can use a regular expression:
if (string.matches("\\w*")) { . . . }
try:
if (string == null || TextUtils.isEmpty(string.trim()) doSomething();
You can use trim to remove whitespace from both ends of the string. Then compare with the empty string.
Kotlin:
The below code takes the string, trims all of the letters down, and checks to see if the result is white space by using .isEmpty().
val str = " "
if (str.trim().isEmpty()) {
// white space only
} else {
// this string has actual characters/letters in it
}
Try: it works
if (txt.getText().toString().trim().matches(" ")){
Toast.makeText(getApplicationContext(), "You did not select any text"
, Toast.LENGTH_LONG).show();
}
else{
}
Related
I'm trying to remove all text tagged like this (including the tags)
<tag>TEXT</tag>
from a String.
I have tried
.replaceAll("<tag>.+/(tag)*>", "")
or
.replaceAll("<tag>.*(tag)*>", "")
but neither works correctly and I can't replace the tagged text with ""
I don't know exactly what you want, so here are a few options:
String text = "ab<tag>xyz</tag>cd";
// Between
text.replaceAll("<tag>.+?<\/tag>", "<tag></tag>"); // ab<tag></tag>cd
// Everything
text.replaceAll("<tag>.+?<\/tag>", ""); // abcd
// Only tags
text.replaceAll("<\/?tag>", ""); // abxyzcd
EDIT:
The problem was the missing ? after the .+. The question mark only matches the first occurence, so it works when multiple tags are present which was the case.
Change to this ,
String nn1="<tag>TEXT</tag>";
nn1=nn1.replace("<tag>","");
nn1=nn1.replace("</tag>","");
OR
String nn1="<tag>TEXT</tag>";
nn1=nn1.replaceAll("<tag>","");
nn1=nn1.replaceAll("</tag>","");
Output : TEXT
I hope this helps you.
public static void removeTAG()
{
String str = "<tag>Your Long String</tag>";
for(int i=0;i<str.length();i++)
{
str = str.replace("<tag>", "");
str = str.replace("</tag>", "");
}
System.out.println(str);
}
Here what i did and output was as expected
Output Your Long String
You can use the below regular expression.
.replaceAll("<tag>.+?<\/tag>", "<tag></tag>");
This removes all the tags whether it's an HTML or an XML tag.
I am trying to ignore spaces in editview between text, I am not quite sure how I can go about doing this. I know I can use trim feature to ignore spaces before and after the full text but how do I ignore space between strings if there is any;
String myTextEdited myText.getText().toString().trim();
For example, if I have / user types in this;
Allan Bob
3523 JKO
NY1 U90
I want to ingore spaces when I read this in my if statement or put it in another variable for example;
String name = "AllanBob"
For example, to ignore upper and lower cases I am doing this;
if (myText.getText().toString().trim().equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
What I would like to do is add another feature in here that also ignores spaces before, between and after text e.g. instead of;
myname is Henry . (space until here)
It should read it as mynameishenry but to the user it still appears as they have written it.
Please let me know if my question was not clear, I will try explaining it better
EDITED:
is it possible to ignore spaces in string that I have inside my if statement. For example;
if (myText.getText().toString().trim().equalsIgnoreCase("Henry 0887")) {
// do something
} else {
// do something
}
but currently if the user types in henry0887, the if statement does not validate it because I added a space inside my validation text and therefoe its looking for a space in the text, is it possible to over come this, so even if I have space inside my validation it ignores it.
Did you try this:
String myString = myEditText.getText().toString();
myString = myString .replace(" ", "");
Hope it helps
EDIT:
if (myText.getText().toString().replace(" ", "").equalsIgnoreCase(userInput) || myText.getText().toString().equalsIgnoreCase(userInput)) {...
Try this,
if(myText.getText().toString().trim().replace(" ","").equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
Hope this helps.
use replaceAll() method.
str = str.replace(" ","");
or for all space chars:
str = str.replace("\\s+","");
EDIT
if (myText.getText().toString().replace("\\s+","").equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
EDIT2
if (myText.getText().toString().replace("\\s+","").equalsIgnoreCase("Henry 0887".replace("\\s+",""))) {
// do something
} else {
// do something
}
I am trying to recognize the initial spaces in my edit text, such that if a user enters " " (any number of spaces) ,it doesnt enable my done button.
So, far I have this code in placE:
String sendString = mSendText.getText().toString();
if(sendString.equals(" ")||sendString.isEmpty()||sendString ==null ){
//do nothing
}else {
//do my stuff
}
The thing is I want the else to work only when I have string with any characters in it as long as it is not JUST ALL whitespace in the beginning.
The code I have works for only 1 whitespace. I want to make it such that no matter how many number of whitespaces exist in the beginning it will remove them or not enable my done button as long as no characters show up.
For example :
This should go to the if loop: " "
This should go to the else loop: " Hello, it's me"
Any ideas?
Thanks!
Just replace equalsTo() to startsWith():
String sendString = mSendText.getText().toString();
if( (sendString == null) || (sendString.startsWith(" ")) || (sendString.isEmpty())){
//do nothing
}else{
//do my stuff
}
Perhaps, if you're interested only in relevant text, you can exclude white spaces in the beginning/ending just using trim()
String sendString = mSendText.getText().toString().trim();
if(sendString.isEmpty()) {
//do nothing
}else{
//do my stuff
}
Use trim() to delete odd spaces from begin and end of String.
String str = new String(" Welcome to Tutorialspoint.com ");
System.out.print("Return Value :" );
System.out.println(Str.trim() );
Returns Welcome to Tutorialspoint.com
I would remove all spaces then check to see if there is anything in the string after that.
String sendString = mSendText.getText().toString().replace(" ","");
if(sendString.isEmpty()){
// do nothing
}else{
// do something
}
I'm trying to make an app where the user enters a word into an EditText box. Then, they enter something into another box and it checks to see if they are the same word. Here's the code that I used:
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
textfield2.setText(textfield2.getText().toString());
if(word == answer){
Toast.makeText(getApplicationContext(), "correct",
Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}
However, it always says that the two strings aren't the same even if they are. Is there a way to fix this?
You can't compare strings with the == operator.
Use .equals() instead:
if(word.equals(answer)) {
//do whatever
}
Use String.equalsIgnoreCase for comparing content of both string variables.:
if(word.equalsIgnoreCase(answer)){
}
Use:
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
if(answer.contentEquals(word)){
// Do something if equals
}
else{
// Do something if not equals
}
I think the best way to do this is using TextUtils:
if(TextUtils.equals(textfield1.getText(),textfield2.getText())){
//do something
}
instead of
if(word.contentEquals(answer)){
}
Use
if(word.equals(answer))
as we cant compare strings with Equal to (==) operator
Try This::
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
if(word.equals(answer)){
Toast.makeText(getApplicationContext(), "correct",
Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}
I have a if condition for check value in textview but it still happen only else condition
what I did wrong, why it doesn't have if condition even if I have set lang=space
private void setLangTitle() {
lang.setText(" ");
db.open();
Cursor cc = db.getLangAct();
cc.moveToFirst();
int index = cc.getColumnIndex(DBAdapter.KEY_LANG);
while (cc.isAfterLast() == false) {
if(lang.equals(" ")){
lang.append("p"+cc.getString(index));
cc.moveToNext();
}
else {
lang.append("/" + cc.getString(index));
cc.moveToNext();
}
}
db.close();
}
You're currently checking if lang is equal to a space character. If lang is a TextView, than it will not be equal to the space character because it is a TextView object, not a string. You probably want to test if the text being displayed on lang is equal to the space character, which would be something along the lines of:
if (lang.getText().toString().equals(" ")) {
...
}
I assume that lang is your TextView object? Then you should use lang.getText().toString()
you should use
if(lang.getText()equals(" ")){
...
}
You were not comparing texts, you were comparing objects. And, of course, a TextView doesn't (shouldn't) compare equal to a String.