Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Searching a string for a single character - how to?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Searching a string for a single character - how to?

    I can't use the 'at' symbol because these forums restrict it, so I'll replace the at symbol with &.

    I'm trying to search a string for the "&" character but it seems as though my regex is not working..

    Here is what I have:

    CharSequence cs = ".*&.*";
    CharSequence cs2 = ".*&a-b.c.d";
     
    boolean csRet = users[i].contains(cs); //Currently everything returns false.
     
    	if (!csRet) {
    		users[i] += "&a-b.c.d";
    	}
     
    boolean csCraRet = users[i].contains(cs2);
     
    	if (cs2Ret) {
                   //Do stuff
            }


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: Searching a string for a single character - how to?

    See the API for the contains method you are using (I am guessing 'users' is an array of String objects) - does it use regular expressions?

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,554
    Thanks
    24
    Thanked 304 Times in 286 Posts

    Default Re: Searching a string for a single character - how to?

    Contains searches for exact Strings, not regexes.
    String does have a "matches" method which accepts regexes, but it checks the ENTIRE String. There are two ways around this:
    1. Add a wildcard to the front and back of your regex and use String.matches(String regex) method (you need to add a wildcard to account for additional text before and after the regex).
    2. Use the Pattern and Matcher classes instead:
    Pattern p = Pattern.compile(regex_pattern); // Creates a new Pattern object with the specified regex
    Matcher m = p.matcher(inputString); // Creates a new Matcher object from the Pattern, with the String you are wanting to search
    boolean exists = m.find(); // Returns true if the regex pattern is found in the String
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  2. [SOLVED] Testing a string for a single character
    By Willsy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 9th, 2013, 05:50 AM
  3. What is the easiest way to change a single character in a string?
    By Jumbosize in forum Java Theory & Questions
    Replies: 2
    Last Post: April 26th, 2012, 04:16 PM
  4. Single Character Validation
    By dannybrgc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 21st, 2012, 06:20 PM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM