0

I am currently working on when a user input a string, then my program will return the search result based on the inputted string. (In this example is "ca")

In the code below, I am only manage to make my program to return all the results which contains lowercase "ca" only.

However, I would like to convert "ca" to all Upper Case while still performing "contains" Regex when returning the search result. Which means, no matter the case is "ca" or "CA", all the results which conatins "CA" will be returned.

May I know how should I modify the code below? Thanks in advance.

public boolean matches( courseList p ) 
{ 
  return p.getName().contains("ca");
}

1 Answer 1

3

Just change your whole text to lower/upper case:

public boolean matches( courseList p ) 
{ 
  return p.getName().toUpperCase().contains("CA");
}

or

public boolean matches( courseList p ) 
{ 
  return p.getName().toLowerCase().contains("ca");
}
Sign up to request clarification or add additional context in comments.

13 Comments

Did you try the code above? For example if p.getName() contains "Capital" then after .toUpperCase() it will be "CAPITAL", so contains "CA".
I tried, but actually what I want is no matter the case inside contains() is "ca" or "CA", all the results which contains "CA" will be returned. For return p.getName().toUpperCase().contains("CA"); if the case inside contains() is "ca" instead, no result will be returned.
Ok, maybe I misunderstand. You have a string as a parameter for the matches() function. You need to know if that string contains ca, Ca, cA or CA (regex independent of case sensitive), correct? if not, give me more information especially with examples.
Yes, that is correct. For example, no matter it is .contains("ca"); or .contains("CA"); all the results which contains both "CA" and 'ca" will be returned. So how should I modify the line return p.getName().contains();?
So my code works correctly for your case. What I've done, I modified input string to form, in which case-sensitivity no matter. If the whole string is upper-case, method .contains("CA") is enough. Note, that I'm not modifying your input - .toLowerCase()/.tuUpperCase() returns new string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.