# 4 by 4 characters from String, right to left

Printable View

• 01-26-2012, 07:07 AM
diamonddragon
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"

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);                    }```
• 01-26-2012, 11:45 AM
Eranga
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.
• 01-27-2012, 02:36 AM
Fubarable
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.
• 01-27-2012, 04:50 AM
diamonddragon
Re: 4 by 4 characters from String, right to left
So, using while loop, this is what I'v got:
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--;       }```
• 01-27-2012, 06:33 AM
Fubarable
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.

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).
• 01-27-2012, 06:46 AM
diamonddragon
Re: 4 by 4 characters from String, right to left
There is a problem, using this:
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.
• 01-27-2012, 06:47 AM
Fubarable
Re: 4 by 4 characters from String, right to left
Quote:

Originally Posted by diamonddragon
There is a problem, using this:
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.
• 01-27-2012, 06:57 AM
diamonddragon
Re: 4 by 4 characters from String, right to left
Quote:

Originally Posted by Fubarable
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.

:(snooze):
You are right. :)-:
• 01-30-2012, 09:03 AM
Eranga
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.
• 01-30-2012, 09:56 AM
DarrylBurke
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.
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
• 01-30-2012, 06:48 PM
diamonddragon
Re: 4 by 4 characters from String, right to left
What does overkill means in this context?
• 01-30-2012, 07:45 PM
diamonddragon
Re: 4 by 4 characters from String, right to left
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);```
• 01-31-2012, 08:56 AM
DarrylBurke
Re: 4 by 4 characters from String, right to left
Quote:

Originally Posted by diamonddragon
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