Java using contains function to match string object ignore capital case?

Short answer: Will not work. You can't overwrite the contains BUT: You can us the following code: List pformats= Arrays. AsList("odt","ott","oth","odm","sxw","stw","sxg","doc","dot","xml","docx","docm","dotx","dotm","doc","wpd","wps","rtf","txt","csv","sdw","sgl","vor","uot","uof","jtd","jtt","hwp","602","pdb","psw","ods","ots","sxc","stc","xls","xlw","xlt","xlsx","xlsm","xltx","xltm","xlsb","wk1","wks","123","dif","sdc","vor","dbf","slk","uos","pxl","wb2","odp","odg","otp","sxi","sti","ppt","pps","pot","pptx","pptm","potx","potm","sda","sdd","sdp","vor","uop","cgm","bmp","dxf","emf","eps","met","pbm","pct","pcd","pcx","pgm","plt","ppm","psd","ras","sda","sdd","sgf","sgv","svm","tgs","tif","tiff","vor","wmf","xbm","xpm","jpg","jpeg","gif","png","pdf","log"); if(pformats.

Contains(extension.toLowerCase())){ } This will make you extension to lowercase, and if within your Array are all extensions are already lowerCase, than it'll wokk.

Short answer: Will not work. You can't overwrite the contains, BUT: You can us the following code: List pformats= Arrays. AsList("odt","ott","oth","odm","sxw","stw","sxg","doc","dot","xml","docx","docm","dotx","dotm","doc","wpd","wps","rtf","txt","csv","sdw","sgl","vor","uot","uof","jtd","jtt","hwp","602","pdb","psw","ods","ots","sxc","stc","xls","xlw","xlt","xlsx","xlsm","xltx","xltm","xlsb","wk1","wks","123","dif","sdc","vor","dbf","slk","uos","pxl","wb2","odp","odg","otp","sxi","sti","ppt","pps","pot","pptx","pptm","potx","potm","sda","sdd","sdp","vor","uop","cgm","bmp","dxf","emf","eps","met","pbm","pct","pcd","pcx","pgm","plt","ppm","psd","ras","sda","sdd","sgf","sgv","svm","tgs","tif","tiff","vor","wmf","xbm","xpm","jpg","jpeg","gif","png","pdf","log"); if(pformats.

Contains(extension.toLowerCase())){ } This will make you extension to lowercase, and if within your Array are all extensions are already lowerCase, than it'll wokk.

As Peter Lawrey mentioned, a Set will be faster. – ckuetbach Jun 21 at 15:14.

A Set is a better choice for a lookup. Private static final Set P_FORMATS = new HashSet(Arrays. AsList( "odt,ott,oth,odm,sxw,stw,sxg,doc,dot,xml,docx,docm,dotx,dotm,doc,wpd,wps,rtf,txt,csv,sdw,sgl,vor,uot,uof,jtd,jtt,hwp,602,pdb,psw,ods,ots,sxc,stc,xls,xlw,xlt,xlsx,xlsm,xltx,xltm,xlsb,wk1,wks,123,dif,sdc,vor,dbf,slk,uos,pxl,wb2,odp,odg,otp,sxi,sti,ppt,pps,pot,pptx,pptm,potx,potm,sda,sdd,sdp,vor,uop,cgm,bmp,dxf,emf,eps,met,pbm,pct,pcd,pcx,pgm,plt,ppm,psd,ras,sda,sdd,sgf,sgv,svm,tgs,tif,tiff,vor,wmf,xbm,xpm,jpg,jpeg,gif,png,pdf,log".

Split(",")); if(P_FORMATS. Contains(extension.toLowerCase())){ // do stuff }.

Convert your List of extensions into a regular expression, compile it with the CASE_INSENSITVE flag, and use that. Import java.util.regex. Matcher; import java.util.regex.

Pattern; public final class Foo { public static void main(final String... args) { final Pattern p = Pattern. Compile("odt|ott|oth|odm|sxw|stw|sxg|doc|dot|xml|docx|docm|dotx|dotm|doc|wpd|wps|rtf|txt|csv|sdw|sgl|vor|uot|uof|jtd|jtt|hwp|602|pdb|psw|ods|ots|sxc|stc|xls|xlw|xlt|xlsx|xlsm|xltx|xltm|xlsb|wk1|wks|123|dif|sdc|vor|dbf|slk|uos|pxl|wb2|odp|odg|otp|sxi|sti|ppt|pps|pot|pptx|pptm|potx|potm|sda|sdd|sdp|vor|uop|cgm|bmp|dxf|emf|eps|met|pbm|pct|pcd|pcx|pgm|plt|ppm|psd|ras|sda|sdd|sgf|sgv|svm|tgs|tif|tiff|vor|wmf|xbm|xpm|jpg|jpeg|gif|png|pdf|log", Pattern. CASE_INSENSITIVE); // Will be true System.out.

Println(p. Matcher("bmp").matches()); // Will be false System.out. Println(p.

Matcher("quasar").matches()); } } This would probably be easier to read/maintain if you build the regex programatically, but I've left that as an exercise to the reader.

Contains() is on Collection so it will work. It's just more efficient on a Set. – artbristol Jun 21 at 10:10 @artbristol: really?

I just edited my answer to say the opposite. Not sure who's right! – Richard H Jun 21 at 10:13 @Richard Contains checks for equality, object identity.

The javadoc for ArrayList says Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null? E==null : o.

Equals(e)).. – Qwerky Jun 21 at 10:20 @Qwerky - ok thanks. I have updated my answer. – Richard H Jun 21 at 10:27 The question was "How to ensure, that the contains()-method will use a lowercase comparision of two Strings" That will not work, expect you create a Deriferd Class, wich implements the contains()-method, But that is not a really good Idea.

Your (and mine) Idea was to change the parameter provided to the contains-method. Thats more easier, than to derive a class. – ckuetbach Jun 21 at 10:36.

If all your formats are lower case, then toLowerCase combined with a HashSet is the preferred solution. If your formats are in mixed case (and shall stay this way, as you are using them for other things, too) you need a real case-insensitive comparison. Then a TreeSet (or other SortedSet) with a case insensitive collator as the comparator will do.(It is not as fast as a HashSet, but will still be faster then the ArrayList (except for really small lists).) Alternatively a HashSet variant using a custom hashCode and equals (or simply a normal HashSet on wrapper objects with a case insensitive implementation of equals and hashCode) would do fine.

Although I'm not sure 100% sure what contains() method will do in this example. You might need to stick your extensions into a Set. Edit: No it won't work as the contains method checks for the existence of a particular Object.

Your string, even with the same value, is a different Object. So yes either a) override the contains method, e. G loop through the array and do a string comparison or b) simpler, use a Set.

Edit 2: Apparently it will work per comments below as ArrayList.contains() checks for equality (so you will get a string match), but this seems to disagree with the top voted answer that says it wont.

Short answer: Will not work. You can't overwrite the contains, BUT: You can us the following code.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions