Results 1 to 9 of 9
Thread: Compare part of a string
- 08-21-2012, 12:36 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Compare part of a string
Hi folks
I need to find a way to compare part of a string to another (ignoring case inlcuded), but can´t find to code for that. Hopefully someone can give a hand.
So if i have a string, that says "Boy, I could sure use lots of cocaine right now", and I want to a positive match if comparing it to a string which is "Cocaine", how should my comparison look?
(sry for the example, it was the first that came to mind :)
- 08-21-2012, 12:55 PM #2
Re: Compare part of a string
Have you read through the API for java.lang.String?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-21-2012, 01:12 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Re: Compare part of a string
Yep, unfortunately i dont understand it, being so new to java :)
Im on a "dicphering other code" state, to figure out what to do. So i was hoping to get a bit of code I could play with, that would help a lot.
- 08-21-2012, 03:55 PM #4
Member
- Join Date
- Jul 2012
- Posts
- 55
- Rep Power
- 0
Re: Compare part of a string
For this you can use the method .contains() from the string class. What it does is takes a sequence of chars and compares them to a string. If that string has the char sequence it returns true.
String (Java 2 Platform SE 5.0)
To make it case insensitive you should look into a method that allows you to control to case of the Strings.Last edited by jhuber151; 08-21-2012 at 04:01 PM.
- 08-21-2012, 03:56 PM #5
Re: Compare part of a string
I'll help you along, but you have to write your own code. To ignore case, you would convert both Strings to either upper or lower case -- String has methods for that. And which method in the API do you think might help you find whether one String contains another String?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-21-2012, 03:58 PM #6
- 08-21-2012, 06:05 PM #7
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Re: Compare part of a string
"contains = checkIn.toLowerCase"
Brilliant. I had found a way, but this is way more clean and simple. Thx a lot.
- 08-21-2012, 11:29 PM #8
Java Rookie
- Join Date
- Aug 2012
- Posts
- 20
- Rep Power
- 0
Re: Compare part of a string
you can also use
Just another possibility you might consider.Java Code:message.indexOf("word")
- 08-22-2012, 09:24 AM #9
Similar Threads
-
Program fails to compare a String Token with a String.
By yasarqamar in forum New To JavaReplies: 4Last Post: 07-11-2012, 12:42 PM -
how to compare one string with another dynamic length pattern string?
By ravi.josih53 in forum New To JavaReplies: 11Last Post: 08-15-2011, 10:12 AM -
Why we use String compare to String Buffer
By Sharath_Forums in forum New To JavaReplies: 1Last Post: 12-06-2010, 06:21 AM -
copying a part of a string and moving it to the end of the string?
By sidd0123 in forum New To JavaReplies: 9Last Post: 04-23-2010, 01:52 AM -
Comparing string using == or != (how to compare string in if else)
By fiqueudrue in forum New To JavaReplies: 6Last Post: 02-10-2009, 06:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks