Android compare string ignore case - android

resultString is the parameter I get from sqlite
resultString = result.getString(result.getColumnIndex(1));
I want to compare ignore case with user input , following is the code I have use. But it doesn't work.
For example, in database, I store a username "George"
When I login, I have to type exactly "George"."george" doesn't work.
Here is my code to compare.
if (userName.equalsIgnoreCase(resultString)) {
return true;
}
What might be the problem?

Please try following code,
if (userName.trim().equalsIgnoreCase(resultString.trim()))
{
return true;
}

Your code should work if the only difference is the case. I suspect you have leading or trailing spaces. Try the following:
if (userName.trim().equalsIgnoreCase(resultString.trim())) {
return true;
}

I was panic and didn't try the to print out the result.The problem was my query statement.
String whereClause = DatabaseHelper.USER_COL + " name =?";
The resultString is always empty unless input is the same as data.
To fix it I follow instruction in this post
sqlite LIKE problem in android
String whereClause = DatabaseHelper.USER_COL + " LIKE ?";
userName = userName+"%"

int Num;
String answer = et_q7.getText().toString();
String right_answer = "Air Pollution";
String right_answer2 = "Air";
if (answer.trim().equalsIgnoreCase(right_answer.trim()) || answer.trim().equalsIgnoreCase(right_answer2.trim())) {
Num = 1;
} else {
Num = 0;
}

Related

What is the best way to fetch email id from group of words?

I am getting group of words as output, is there any way to fetch email id's from that group.
My output will be like this.
Lakshman Kumar,D/no:45/24/d4,USA,Android app devoloper,lakshman#gmail.com
If there are one or more email addresses, split the text and match the pattern of email as below:
String[] tokens = yourText.split(",");
for (String token : tokens) {
if (Patterns.EMAIL_ADDRESS.matcher(token).matches()) {
String email = token;
//use this email
}
}
you need to use regex for fetching email id from text like this
Pattern p = Pattern.compile("\\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\\.[A-Z]{2,4}\\b",
Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher("your text will be here ");
Set<String> emails = new HashSet<String>();
while(matcher.find()) {
emails.add(matcher.group());
}
if you are sure that "#" will always be used only inside email addresses, you could do this:
String output = "Lakshman Kumar,D/no:45/24/d4,USA,Android app devoloper,lakshman#gmail.com";
public String getEmail(){
// Splitting your output by ','
String[] splittedOutput = output.split(",");
for (String s : splittedOutput){
// Checking to see if '#' exists in string
if (s.indexOf("#") >= 0){
return s;
}
}
return "email not found";
}
Try this .
String response = "Lakshman Kumar,D/no:45/24/d4,USA,Android app devoloper,lakshman#gmail.com";
String[] strings = response.split(",");
String email = strings[strings.length - 1];
Log.e("TAG", email);
NOTE
Use split in your code
Use strings[strings.length - 1] to get value of email

how to start count upto 1 until press comma(,) it should count tow

its may be silly but am confused on that this i want to start count up to one and if press comma(,)then i want to count comma only, here how i am try.
String conCount;
conCount = "1";
int countComma = conCount.length() - conCount.replace(",", "").length();
String lenVar;
lenVar = conCount;
convert = String.valueOf(countComma);
if (conCount.length() == 0) {
lenVar = "0";
} else {
textViewConCount.setText(convert);
}
String editTextString = "abc,efg,pqr,xyz";
if (editTextString.contains(",")) {
int countStringsSeperatedByComma = 0;
countStringsSeperatedByComma = editTextString.split(",").length;
System.out.println("Count of strings seperated by comma : " + countStringsSeperatedByComma);
int commaCount = countStringsSeperatedByComma - 1;
System.out.println("Count of commas : " + commaCount);
} else {
System.out.println("Count of characters in editText string : " + editTextString.length());
}
Output for above condition will be :
Count of strings seperated by comma : 4
Count of commas : 3
Suppose if your string is "abcefgpqrxyz" i.e. without comma then it will execute else part and print characters count as 12 in this case
Count of characters in editText string : 12
Your question statement is so ambiguous. Elaborate it completely and explain your end result with example. It's regarding string functions, I can give you the answer about it if I understand it :) :D
thanks for that i got that answer like that.
String varStr = editextContact.getText().toString();
//int VarCount = editextContact.getText().length();
int countStringsSeperatedByComma = varStr.split(",").length;
String convet=String.valueOf(countStringsSeperatedByComma);
textViewConCount.setText(varStr);
if (varStr.length() == 0){
textViewConCount.setText("0");
}else {
textViewConCount.setText(convet);
}

Android : replacing characters in a string

In my phonebook on my mobile I have all sorts of contacts like :
+(353) 085 123 45 67
00661234567
0871234567
(045)123456
I'm putting them all into E.164 format which I've largely completed but the question I need resolved is this:
How can I strip all characters (including spaces) except numbers in my string, apart from the first character if it is '+' or a number ?
string phoneNumberofContact;
So for example the cases above would look like :
+3530851234567
00661234567
0871234567
045123456
Update
To handle + only in the first position, you could do:
boolean starsWithPlus = input.charAt(0) == '+';
String sanitized = input.replaceAll("[^0-9]", "");
if (startsWithPlus) {
sanitized = "+" + sanitized;
}
So basically I'm checking to see if it starts with plus, then stripping out everything but digits, and then re-adding the plus if it was there.
Original
Assuming you only want to keep + or digits, a simple regex will work, and String provides the replaceAll() method to make it even easier.
String sanitized = input.replaceAll("[^+0-9]", "");
This method would do the trick
public String cleanPhoneDigits(String phonenum) {
StringBuilder builder = new StringBuilder();
if (phonenum.charAt(0).equals('+') {
builder.append('+');
}
for (int i = 1; i < phonenum.length(); i++) {
char c = phonenum.charAt(i);
if (Character.isDigit(c)) {
builder.append(c);
}
}
return builder.toString();
}

How to detect if a string contains a specific Word

I have this code :
if (currentLocation.distanceTo(myModel.getNearest()) < 900) {
if (said != true) {
String seriousWarning = (myModel.getNearest().getProvider());
tts.speak(seriousWarning, TextToSpeech.QUEUE_ADD, null);
said = true;
warningTxt.setTextColor(Color.RED);
}
I would like to check if there a certain word in the seriousWarning string, knowing that (myModel.getNearest().getProvider()) is the title of the nearest GPS point to the device.
Any help would be much appreciated!
try below piece of code:
boolean isPdf = stringValue.matches(".*\\b"STRING_NAME"\\b.*");
You can use contains() method.
if(seriousWarning.contains("certainword"))
{
//Do something
}
You can use regular expressions to check if a string contains a substring.
This code snippet is from the android developer documentation.
// String convenience methods:
boolean sawFailures = s.matches("Failures: \\d+");
String farewell = s.replaceAll("Hello, (\\S+)", "Goodbye, $1");
String[] fields = s.split(":");
// Direct use of Pattern:
Pattern p = Pattern.compile("Hello, (\\S+)");
Matcher m = p.matcher(inputString);
while (m.find()) { // Find each match in turn; String can't do this.
String name = m.group(1); // Access a submatch group; String can't do this.
use indexOf("String to be checkecked");
if(seriousWarning.indexOf("String to be checkecked") > -1)
{
// your code
}

Android - Check content of a String

I have a string (length 3-8) assigned to a variable (text). I want to check whether the 2nd and 3rd characters are NOT numeric (a letter or symbol or space..or anything other than numbers).
Elementary way to do this could be:
if(((text.charAt(1)-'0')>=0)&&(text.charAt(1)-'0')<10))||((text.charAt(2)-'0')>=0)&&(text.charAt(2)-'0')<10)))
{
//do nothing, since this means 2nd and/or 3rd characters in the string are numeric
}
else
{
// Your condition is met
}
You could also use REGEX's , if your checking is still more complicated.
Here is Another way to achieve this:
boolean isNumeric = true;
String test = "testing";
char second = test.charAt(1);
char third = test.charAt(2);
try {
Integer.parseInt(String.valueOf(second));
Integer.parseInt(String.valueOf(third));
} catch(NumberFormatException e) {
isNumeric = false;
}
System.out.println("Contains Number in 2nd and 3rd or both position: " + isNumeric);
You might make use of the String.IndexOf(String) method, like:
String digits = "0123456789";
String s2 = text.substring(2,3);
String s3 = text.substring(3,4);
boolean valid = (digits.indexOf(s2) > -1) && (digits.indexOf(s3) > -1);

Categories

Resources