Results 1 to 9 of 9
  1. #1
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default Find a string within a string

    Hi,

    I have a string A= "Amsterdam"

    There is another string B= "amst"

    How do I find if String A contains B.
    I tried using the contains method but that is case sensitive.
    I also tried StringA.indexOf(StringB)>0 but this does not work if String A has
    a uppercase 'A' and string B with lowercase a.

    If String B="amst" or "Amst"(with uppercase)
    It should work.

    Please help me how to achieve this .

    Thanks,
    Manshu

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by pink123 View Post
    Hi,

    I have a string A= "Amsterdam"

    There is another string B= "amst"

    How do I find if String A contains B.
    I tried using the contains method but that is case sensitive.
    I also tried StringA.indexOf(StringB)>0 but this does not work if String A has
    a uppercase 'A' and string B with lowercase a.

    If String B="amst" or "Amst"(with uppercase)
    It should work.

    Please help me how to achieve this .

    Thanks,
    Manshu
    Take care that both Strings are all in uppercase (or lowercase).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default

    Thanks for your reply.
    Do you mean I convert the strings into uppercaser or lower case before comparing .String B will be users input and string A is data from some table.
    I have to perform certain steps if String A contains String B.

    Thanks,

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by pink123 View Post
    Thanks for your reply.
    Do you mean I convert the strings into uppercaser or lower case before comparing .String B will be users input and string A is data from some table.
    I have to perform certain steps if String A contains String B.

    Thanks,
    Yup, if you have the Strings "Amsterdam" and "amst" you have to compare the Strings "AMSTERDAM" and "AMST" or "amsterdam" and "amst". The String class has the necessary methods to do the conversion.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    http://download.oracle.com/javase/6/...owerCase%28%29
    http://download.oracle.com/javase/6/...pperCase%28%29

    or you could use regex :p

    something like
    Java Code:
    stringOne.matches(".*(?i)"+stringTwo+".*")
    ;)

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by eRaaaa View Post
    String (Java Platform SE 6)
    String (Java Platform SE 6)

    or you could use regex :p

    something like
    Java Code:
    stringOne.matches(".*(?i)"+stringTwo+".*")
    ;)
    Too dangerous; imagine what would happen if stringTwo contains (part of) a regular expression, e.g. "[".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default

    thanks everyone for their reply.Its working now with the regular expression you provided.

    Thanks

  8. #8
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default

    String B is users name so it will not contain those characters

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

    Default

    Quote Originally Posted by pink123 View Post
    String B is users name so it will not contain those characters
    Never rely on assumptions like that ;)
    xkcd: Exploits of a Mom

    db

Similar Threads

  1. How to find connection string of Database?
    By vishvadeepaktripathi in forum JDBC
    Replies: 0
    Last Post: 02-25-2011, 08:47 AM
  2. Replies: 2
    Last Post: 03-27-2010, 07:22 PM
  3. find index of string in another string
    By Sdannenberg3 in forum New To Java
    Replies: 4
    Last Post: 03-04-2010, 10:14 AM
  4. Find a number from a string
    By florentp in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 09:01 PM
  5. Find and replace ( in a String
    By hamish10101 in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 05:51 AM

Posting Permissions

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