Results 1 to 6 of 6

Thread: String.contains

  1. #1
    marksey07 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default String.contains

    Can anyone tell me how i would write the java alternative for-

    String.contains(either "(" or ")");

    thanks

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    See String (Java Platform SE 6), in particular the indexOf, contains, etc...methods

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

    Default

    String.contains(either "(" or ")");
    One way: use two contains(...) method calls and OR (that is, ||) the result.
    Another way: use an appropriate regular expression and a call to matches(...).

    More about regexes in the documentation for Pattern and here:
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns

    db

  4. #4
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    if you are trying to execute a conditional block you can use an if statement combined with an || (or) as follows:
    Java Code:
    if(String.contains("(") || String.contains(")")) {
        do something;
    }
    Best,
    --user0--

  5. #5
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by marksey07 View Post
    Can anyone tell me how i would write the java alternative for-

    String.contains(either "(" or ")");

    thanks
    Java Code:
            string.matches(".*[()].*")

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  2. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  3. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  4. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  5. Replies: 5
    Last Post: 01-11-2009, 10:12 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
  •