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

1. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
5

## 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. ## 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.

3. ## 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. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
5

## 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. ## 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;
binaryString = binaryString.substring(0, index);
}
if (binaryString.length() > 0) {
}

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).

6. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
5

## 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. ## Re: 4 by 4 characters from String, right to left

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

8. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
5

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

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.

You are right.

9. ## 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. ## 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

11. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
5

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

What does overkill means in this context?

12. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
5

## 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. ## Re: 4 by 4 characters from String, right to left

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

#### Posting Permissions

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