Results 1 to 9 of 9
  1. #1
    haddock256 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Question 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 :)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Compare part of a string

    Have you read through the API for java.lang.String?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    haddock256 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  4. #4
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default 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?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Compare part of a string

    Quote Originally Posted by jhuber151 View Post
    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 just add on a toLowerCase() to both strings, so it would look similar to
    Java Code:
    String checkIn = "The cat ran in the house";
    String compareTo = "cat";
    boolean contains = checkIn.toLowerCase().contains(compareTo.toLowerCase());
    System.out.println(contains);
    jhuber151, please don't spoonfeed. Give people a chance to learn.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    haddock256 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  8. #8
    bob 6 is offline Java Rookie
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Default Re: Compare part of a string

    you can also use
    Java Code:
    message.indexOf("word")
    Just another possibility you might consider.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Compare part of a string

    Quote Originally Posted by bob 6 View Post
    you can also use
    Java Code:
    message.indexOf("word")
    Just another possibility you might consider.
    No, you can't. Read the question in the first post, carefully.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2014, 08:06 AM
  2. Program fails to compare a String Token with a String.
    By yasarqamar in forum New To Java
    Replies: 4
    Last Post: 07-11-2012, 12:42 PM
  3. Replies: 11
    Last Post: 08-15-2011, 10:12 AM
  4. Why we use String compare to String Buffer
    By Sharath_Forums in forum New To Java
    Replies: 1
    Last Post: 12-06-2010, 06:21 AM
  5. Replies: 9
    Last Post: 04-23-2010, 01:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •