aspose file tools*
The moose likes Java in General and the fly likes Find String that contains a given substring ignoring case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Find String that contains a given substring ignoring case" Watch "Find String that contains a given substring ignoring case" New topic
Author

Find String that contains a given substring ignoring case

Udaya Wijenayake
Ranch Hand

Joined: May 15, 2009
Posts: 30
How to find a string that contains given substring ignoring case?
I have used following code. But it considers the case.



SCJP 1.5
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

A possible option would be, to first convert your base String to lowercase(or uppercase) and then use substring on it.


OCPJP 6
Udaya Wijenayake
Ranch Hand

Joined: May 15, 2009
Posts: 30
Thanks vinoth, I got it
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Using regex is overkill for such a simple requirement.


luck, db
There are no new questions, but there may be new answers.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Darryl Burke wrote:Using regex is overkill for such a simple requirement.


but it is flexible . what could you do if the string needs to be lowercase? you need to apply one more condition right?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

But that isn't the requirement. Using a regex *is* overkill for the requirement as stated.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

But calling toUpperCase() on a large String (several thousands characters) may have a negative impact on memory usage. This is one of those cases where you must make a trade off between better speed (toUpperCase(), contains()) and less memory (Pattern). If this operation is needed many times reusing a Pattern object may even be better.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Meh--if the file name has thousands of characters I'd suggest there's a different problem waiting in the wings.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find String that contains a given substring ignoring case