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

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

Related

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.

Regex to extract 4-digit number from a String - Android [duplicate]

This question already has answers here:
Creating regex to extract 4 digit number from string using java
(8 answers)
Closed 6 years ago.
I need to extract a 4-digit number from a string:
e.g "Your otp for the login is 7832. This code will expire at 12:43:09PM" from SMS in Android
I want to extract 7832 or any 4-digit code that comes within the string. I ensure that there will be only one 4-digit code in the string.
Please help me. I trying to use patterns like:
str.matches(".*\\\\d+.*");
But I'm not able to understand regexes much.
String data = "Your otp for the login is 7832. This code will expire at 12:43:09PM";
Pattern pattern = Pattern.compile("(\\d{4})");
// \d is for a digit
// {} is the number of digits here 4.
Matcher matcher = pattern.matcher(data);
String val = "";
if (matcher.find()) {
val = matcher.group(0); // 4 digit number
}
Do:
\b\d{4}\b
\b matches word boundary
\d{4} matches 4 digits
Demo

Firebase - How can I filter similarly to equalTo() but instead check if it contains the value? [duplicate]

This question already has answers here:
Firebase complex "contain" queries
(2 answers)
Closed 6 years ago.
Let's say I have this database:
J-DSKFJASKDFJASKDJ:{
username: "Burned"
},
J-KASDJFAKSDJFKDSJ:{
username: "BurnedFirebase"
},
J-DFJKADSJFAKSDJFK:{
username: "FirbaseFilteringIsVeryLackingThereforItSucks"
}
If I use this:
Query q = database.orderByChild("username").equalTo("Burned");
It will actually return only "Burned", although there is a BurnedFirebase in the database. That's because equalTo() doesn't work as a method such as contains().
Is there a method similar to contains()? If not, is there a workaround?
If your search is always at the beginning of the word (you're not searching for ned and expecting Burned to come up), you can use a high unicode character:
query.startAt("Burned").endAt("Burned\uf8ff")
From: https://firebase.google.com/docs/database/admin/retrieve-data#range-queries
...dinosaursRef.orderByKey().startAt("b").endAt("b\uf8ff")...
The \uf8ff character used in the query above is a very high code point
in the Unicode range. Because it is after most regular characters in
Unicode, the query matches all values that start with a b.

removing case senstivity from a string [duplicate]

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

Remove decimal point and numbers from string in Android [duplicate]

This question already has answers here:
How to remove the " .0" in a whole number when using double in java? [duplicate]
(3 answers)
Closed 4 years ago.
So, I have two numbers as strings - for example, 123.00 and 123.50. How do I remove the decimal point and all that follows. I had a method that worked for the 123.00, but would not work correctly on the 123.50.
Here is what I have so far:
String balance = getString("balance");
BigDecimal number = new BigDecimal(balance);
String formattedBalance = number.stripTrailingZeros().toPlainString();
int roundedBalance = Integer.parseInt(formattedBalance);
int roundedBalance = Integer.parseInt(getString("balance").split(".")[0]);
Do you want to remove the following points or numbers?
If only the points why not:
balance = balance.replace(".","");

Categories

Resources