Option to ignore case with .contains method?
I'm guessing you mean ignoring case when searching in a string?
I don't know any, but you could try to convert the string to search into either to lower or to upper case, then search.
// s is the String to search into, and seq the sequence you are searching for.
bool doesContain = s.toLowerCase().contains(seq);
Edit:
As Ryan Schipper suggested, you can also (and probably would be better off) do seq.toLowerCase(), depending on your situation.
ArrayList & contains() - case insensitive
No. You will have to define your own contains method that will have to iterate among the ArrayList
and compare the values using the equalsIgnoreCase
method of the String
class.
Edit: I don't want to be rude, but the question is pretty clear: the guy wants to use the contains
method. So he can't/should use toLowerCase
before adding the elements because of too many reasons: for example, he could need the original String
(not the one that is lowercased). Also, as we are talking about the contains
method, we are focusing on the elements rather than their indexes (as someone answered some minutes ago).
ArrayList contains case sensitivity
You can use this exactly like you'd use any other ArrayList. You can pass this List out to other code, and external code won't have to understand any string wrapper classes.
public class CustomStringList3 extends ArrayList<String> {
@Override
public boolean contains(Object o) {
String paramStr = (String)o;
for (String s : this) {
if (paramStr.equalsIgnoreCase(s)) return true;
}
return false;
}
}
Checking if an ArrayList contains a certain String while being case insensitive
You will need to iterate over the list and check each element. This is what is happening in the contains
method. Since you are wanting to use the equalsIgnoreCase
method instead of the equals
method for the String elements you are checking, you will need to do it explicitly. That can either be with a for-each loop or with a Stream (example below is with a Stream).
private final List<String> list = new ArrayList<>();
public void addIfNotPresent(String str) {
if (list.stream().noneMatch(s -> s.equalsIgnoreCase(str))) {
list.add(str);
}
}
How to make use of Case-Insensitive Contains in ArrayList
If you are using .NET 1.1 because Generics aren't available, you could do something like this:
bool contains = false;
for (int i = 0; i < myArrayList.Count && !contains; i++)
{
contains = ((string)myArrayList[i]).ToUpper() == "APPLE";
}
if (contains)
{
statusLabel.Text = "ArrayList contains apple";
}
Otherwise, using List<string>
instead of ArrayList, then myArrayList.Contains("apple", StringComparer.OrdinalIgnoreCase)
will work.
If you have .NET 2 + Generics available to you; then there really is no good reason to be using the ArrayList
anymore.
Make ArrayList element case-insensitive
Why not using instead a SortedSet with a case insensitive comparator ?
With the String.CASE_INSENSITIVE_ORDER comparator
Your code is reduced to
Set<String> a = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
a.add("one");
a.add("three");
a.add("two");
Set<String> a1 = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
a1.add("ONE");
a1.add("two");
a1.add("THREE");
And your equals conditions should work without any issue
EDIT modified according to comments. Thanks to all of you to correct me.
Array contains() without case sensitive lookup?
contains
just check if an object is present in the List. So you can't do a case insensitive lookup here, because "three" is a different object than "Three".
A simple approach to solve this would be
public boolean containsCaseInsensitive(String s, List<String> l){
for (String string : l){
if (string.equalsIgnoreCase(s)){
return true;
}
}
return false;
}
and then
containsCaseInsensitive("three", data);
Java 8+ version:
public boolean containsCaseInsensitive(String s, List<String> l){
return l.stream().anyMatch(x -> x.equalsIgnoreCase(s));
}
Search Array list case insensitive
anyMatch
only returns true
or false
indicating whether there is at least 1 element in the stream that satisfies the predicate.
You should use filter
and findFirst
, which returns the first element that matches the predicate:
speciality = specialities.stream()
.filter(spec::equalsIgnoreCase)
.findFirst()
.orElse("General Practitioner");
Removing first case insensitive duplicate of an ArrayList
I think that remove from the ArrayList
is not good idea. It is better using Map
to create new list:
public static List<String> removeDuplicates(List<String> strList) {
Map<String, String> map = new LinkedHashMap<>();
strList.forEach(item -> map.put(item.toLowerCase(), item));
return new ArrayList<>(map.values());
}
Input: [interface, list, Primitive, class, primitive, List, Interface, lIst, Primitive]
Output: [Interface, lIst, Primitive, class]
P.S.
Same with one line Stream
, but a bit not so clear:
public static List<String> removeDuplicates(List<String> strList) {
return new ArrayList<>(strList.stream().collect(Collectors.toMap(String::toLowerCase, str -> str, (prev, next) -> next, LinkedHashMap::new)).values());
}
How to check if a String contains another String in a case insensitive manner in Java?
Yes, contains is case sensitive. You can use java.util.regex.Pattern with the CASE_INSENSITIVE flag for case insensitive matching:
Pattern.compile(Pattern.quote(wantedStr), Pattern.CASE_INSENSITIVE).matcher(source).find();
EDIT: If s2 contains regex special characters (of which there are many) it's important to quote it first. I've corrected my answer since it is the first one people will see, but vote up Matt Quail's since he pointed this out.
Related Topics
Why Are Interface Method Invocations Slower Than Concrete Invocations
Java Error: Cannot Make a Static Reference to the Non-Static Method
Copying Text to the Clipboard Using Java
Spring Session-Scoped Beans (Controllers) and References to Services, in Terms of Serialization
Download Attachments Using Java Mail
How to Decode a String Encoded with Openssl Aes-128-Cbc Using Java
How to Enable Anti Aliasing in Arbitrary Java Apps
How to Deal with the Urisyntaxexception
Differencebetween a Hashmap and a Treemap
Joda-Time: What's the Difference Between Period, Interval and Duration
Logging User Activity in Web App
Difference Between an Application Server and a Servlet Container
Chat Client Emoticons Window Java
Javamail with Gmail: 535-5.7.1 Username and Password Not Accepted
Java Parsing Xml Document Gives "Content Not Allowed in Prolog." Error