removing case senstivity from a string [duplicate] - android

This question already has answers here:
How do I make my string comparison case-insensitive?
(12 answers)
Closed 6 years ago.
i perform a special action if the string equals something. However, i want to perform the same action if the string has uppercase characters. but i dont want to write so many else if statements for every case-
decrypt
Decrypt
DECRYPT
} else if (message.equals("decrypt")) {
removeencryption(context);
is there a way of removing case sensitivity?
i have already tried
} else if (message.toLowerCase().equals("whistle")) {
whistle(context);
but it is not working

Use equalsIgnoreCase method of String class:
else if (message.equalsIgnoreCase("decrypt")) {
removeencryption(context);
}

Related

My currenct regex is not applying to my price [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
How to use regex to find a keyword in Kotlin
(2 answers)
Closed 25 days ago.
I'm trying to format a crypto currency price with the follow regex
fun Double.toPrice(): String {
val regex = Regex("(0.0*\\d{1,3})")
val match = regex.matches(this.toString())
return if(match) {
DecimalFormat(regex.pattern).format(this)
} else {
NumberFormat.getCurrencyInstance(Locale("en", "US")).format(this)
}
}
this regex works as expected as I saw in here https://regex101.com/r/0tFLiR/1
This regex should only get latest 3 numbers after x amount of 0 places after 0. in my numbers.
But this is not working as the if condition always yield false
how should I make this work ?
This is extracted from this answer in C# but I cannot make it work in my example

case senstive search with realtime database and recylcerview [duplicate]

This question already has answers here:
Android: Firebase Search Query not working properly
(2 answers)
Closed 1 year ago.
private void processsearch(String s)
{
FirebaseRecyclerOptions<Calories> options =
new FirebaseRecyclerOptions.Builder<Calories>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Food").orderByChild("Name").startAt(s).endAt(s+"\uf8ff"), Calories.class)
.build();
adapter=new myadapter(options);
adapter.startListening();
recview.setAdapter(adapter);
}
how can I make this search method case insensitive?
I've tried doing
String n="Name";
n.equalsIgnoreCase()
; then replaced the word name in orderbychild with n but it didn't work
I've also tried toLowerCase on n, I can't seem to find the method .equalsIgnoreCase() inside the recylcerOptions that's why i tried to do it on the outside
When you store your strings in your database, you should use a method to make it all lower case, such as string.toLowerCase().
And when you query, you convert your query to lower case as well query.toLowerCase()
there are other factors to consider such as special characters that should be removed/ignored but that is out of the scope of this question.

How to add email address as child in Firebase? [duplicate]

This question already has answers here:
Good way to replace invalid characters in firebase keys?
(4 answers)
Closed 4 years ago.
I am working on an Android App where I want a Firebase database like
Basically, I want to verify email before creating a user on the app. Whenever a user registers in my app, First it will be checked whether the email address is available in the users database reference. if available, then only he is allowed to register.
But I found that Firebase doesn't allow an email as a child. So How can I do it? Any other suggestion for achieving this in Firebase Database.
Because Firebase does not allow symbols as . in the key, I suggest you encode the email address like this:
name#email.com -> name#email,com
As you probably see, instead of . I have used ,. To achieve this, you can use the following methods:
static String encodeUserEmail(String userEmail) {
return userEmail.replace(".", ",");
}
static String decodeUserEmail(String userEmail) {
return userEmail.replace(",", ".");
}

How to detect device model? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am trying to detect the device model and trying to do some operations based on that.
I do the code to detect the device model and manufacturer but for some reason its giving me a negative result.
Here's the code to detect the device model and manufacturer:
private Boolean isHTCone(){
//For model and manufacturer both
String HTC_MODEL_MANUFACTURER = android.os.Build.MODEL+""+android.os.Build.MANUFACTURER;
//If true then do something
Boolean isHTC = true;
//My model name
String HTCModel = "HTC6500LVW";
//For device model
String MyBuildModel = android.os.Build.MODEL;
//For manufacturer
String MyBuildManufacturer = android.os.Build.MANUFACTURER;
//Check for the condition
if(MyBuildModel==HTCModel){
Log.i("Device Model is: ", android.os.Build.MODEL);
return true;
}
else{
Log.i("Device is not HTC One");
return false;
}
}
Its always giving me a wrong value. Even when my device model is "HTC6500LVW", it's not accepting the if loop and is going to the else.
Can anyone tell me if I am doing something wrong here. I will really appreciate it.
Thanks in advance..:)
Do like this
if(MyBuildModel.equals(HTCModel)) {
// your code
}
== operator compare the reference of two string which in your case is different.
In Java, to compare the strings, you have to use the equals() method for this.
You'll have to use String.equals() to compare your strings, like this:
if (MyBuildModel.equals(HTCModel)){
Otherwise, using == you are comparing the memory adresses of the Strings, which of course are not the same.
As a side note, variable names should start with a lower case letter.

Access the values of res/values/strings [duplicate]

This question already has answers here:
Difference between getString() and getResources.getString()
(3 answers)
Closed 9 years ago.
I read about String Resources and I understood that you simply use the getString(...) method in order to read the value of a string from res/values/string.xml. Then I read that you can also use getResources().getString(...).
What is the difference between these two ways of obtaining the value of a string?
No any difference. They're both equal.

Categories

Resources