Results 1 to 13 of 13
Like Tree5Likes
  • 1 Post By Eranga
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 2 Post By DarrylBurke

Thread: 4 by 4 characters from String, right to left

  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 4 by 4 characters from String, right to left

    I'm trying to get sequences of 4 characters from String, from right to left, such that say if String.length() is 15, remaining sequence consist of 15%4 characters.

    Here is code, and my question is, is there a way to do this without these if statements?

    Say binaryString = "100010010011101"

    Java Code:
    String binaryString = "100010010011101";
    
            for (int i = binaryString.length(); i > 0; i -= 4) {
                String tempBinary = null;
                int start = i - 4;            
                int end = i;
                
                if (start < 0)
                    start = 0;
                
                if (end == binaryString.length())
                    tempBinary = binaryString.substring(start);
                else
                    tempBinary = binaryString.substring(start, end);            
            }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: 4 by 4 characters from String, right to left

    Split the string in a while loop and validate each sub-string for that the length is less than 4 or not.
    Fubarable likes this.

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

    Default Re: 4 by 4 characters from String, right to left

    Yep, no need to even use a for loop -- just use a while loop. Note though that if you're going through a large String, consider using a StringBuilder instead of String to avoid creation of multiple unnecessary String objects.

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: 4 by 4 characters from String, right to left

    So, using while loop, this is what I'v got:
    Java Code:
           String binaryString = "100010010011101";       
           StringBuilder buffer = new StringBuilder(4);       
           String output;
           
           int count = binaryString.length() - 1;
           int bufferCount = 0;
           while (count >= 0) {
               char c = binaryString.charAt(count);
               buffer.append(c);
               bufferCount++;
               if (bufferCount % 4 == 0 || count == 0) {
                   buffer.reverse();
                   output = buffer.toString();
                   buffer.delete(0, 4);               
                   bufferCount = 0;
               }                           
               count--;
           }

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

    Default Re: 4 by 4 characters from String, right to left

    If all you're manipulating is that little String, then there's no need to use a StringBuffer or StringBuilder. Something as simple as this could work.

    Java Code:
       public static void main(String[] args) {
          String binaryString = "100010010011101"; 
          List<String> tokens = new ArrayList<String>();
          
          while (binaryString.length() > 4) {
             int index = binaryString.length() - 4;
             tokens.add(binaryString.substring(index));
             binaryString = binaryString.substring(0, index);
          }
          if (binaryString.length() > 0) {
             tokens.add(binaryString);
          }
          
          for (String token : tokens) {
             System.out.println(token);
          }
       }
    If you are manipulating large Strings, then yes, use a StringBuilder (or StringBuffer if you are using multiple threads and have a risk of a thread clash).
    diamonddragon likes this.

  6. #6
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: 4 by 4 characters from String, right to left

    There is a problem, using this:
    Java Code:
    while (binaryString.length() > 4) {
    last token, which length is less than 4, will not be considered.
    But I see the point, thanks for effort.

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

    Default Re: 4 by 4 characters from String, right to left

    Quote Originally Posted by diamonddragon View Post
    There is a problem, using this:
    Java Code:
    while (binaryString.length() > 4) {
    last token, which length is less than 4, will not be considered.
    But I see the point, thanks for effort.
    You haven't read the code below the while loop have you? Read the whole post and ask for clarification about code that you don't understand before making these types of replies.
    Last edited by Fubarable; 01-27-2012 at 06:55 AM.
    diamonddragon likes this.

  8. #8
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: 4 by 4 characters from String, right to left

    Quote Originally Posted by Fubarable View Post
    You haven't read the code below the while loop have you? Read the whole post and ask for clarification about code that you don't understand before making these types of replies.

    You are right.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: 4 by 4 characters from String, right to left

    Don't be lazy yo do such things. If you really want to learn this kind of stuff, read it again and again and practice them continuously.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: 4 by 4 characters from String, right to left

    Using overkill because Java regex doesn't support a look behind that doesn't have a predetermined maximum length.
    Java Code:
    import java.util.Arrays;
    import java.util.List;
    
    public class Split4s {
    
      public static void main(String[] args) {
        String input = "12345678901234567890123456";
        List<String> list = Arrays.asList(input.split("(?<=^(.{4}){1,100000000})"));
        System.out.println("Split in " + list.size() + " parts\n" + list);
      }
    }
    db
    Fubarable and diamonddragon like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: 4 by 4 characters from String, right to left

    What does overkill means in this context?

  12. #12
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: 4 by 4 characters from String, right to left

    Java Code:
             String input = "123456789012345678";
             ArrayList<String> list = new ArrayList();
             
             for (int i = input.length(); i > 0; i -= 4)
                 list.add(input.substring(i > 4 ? i - 4 : 0, i));            
             System.out.println("Split in " + list.size() + " parts\n" + list);
    Last edited by diamonddragon; 01-30-2012 at 08:16 PM.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: 4 by 4 characters from String, right to left

    Quote Originally Posted by diamonddragon View Post
    What does overkill means in this context?
    The regex has a look-behind for four characters, repeated 1 to 100000000 times. The 100000000 is overkill as it can cater to a String of 400000000 characters.

    On some platforms (not Java), variable length look behinds are permitted in regex, and you could use (.{4}){1,}.

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

Similar Threads

  1. Reversing dojo slider to make it slide from right to left rather left to right as giv
    By gurpreet.singh in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-05-2011, 02:49 PM
  2. square moves left and down but not up or left
    By natdizzle in forum AWT / Swing
    Replies: 3
    Last Post: 02-04-2011, 06:20 PM
  3. Replies: 1
    Last Post: 04-30-2010, 04:21 PM
  4. how to get the characters one by one from a String?
    By Somitesh Chakraborty in forum New To Java
    Replies: 3
    Last Post: 08-20-2008, 09:56 PM
  5. Getting all characters in a String
    By Alayna in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 12:49 PM

Posting Permissions

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