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: String actual and next char

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String actual and next char

    Hi, I need to browse string by char and have access to the current and next character.
    But I don't want to use any counter.

  2. #2
    Junior Member
    Join Date
    Mar 2020
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String actual and next char

    You could do something like this:

    String s = "abcde";
    CharacterIterator it = new StringCharacterIterator(s);
     
    while(it.current() != CharacterIterator.DONE) {
        char current = it.current();
        char next = it.next();
        it.next();
    }
    Just be sure to check next for equality with CharacterIterator.DONE if you have an odd number of characters in your string.
    Last edited by lodenrogue; March 29th, 2020 at 09:34 AM. Reason: Code tag

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String actual and next char

    Can you put the posted code in code tags to preserve formatting and show highlights?
    If you don't understand my answer, don't ignore it, ask a question.

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. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  3. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 10:04 AM
  4. string-int-char
    By mdhmdh100 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 25th, 2012, 01:35 PM
  5. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM