Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find String that contains a given substring ignoring case

 
Udaya Wijenayake
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to find a string that contains given substring ignoring case?
I have used following code. But it considers the case.


 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A possible option would be, to first convert your base String to lowercase(or uppercase) and then use substring on it.
 
Udaya Wijenayake
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks vinoth, I got it
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using regex is overkill for such a simple requirement.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that isn't the requirement. Using a regex *is* overkill for the requirement as stated.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meh--if the file name has thousands of characters I'd suggest there's a different problem waiting in the wings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic