Results 1 to 13 of 13
5Likes Thread: 4 by 4 characters from String, right to left
- 01-26-2012, 06:07 AM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
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); }
- 01-26-2012, 10:45 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
-
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, 03:50 AM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
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--; }
-
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.
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).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); } }
- 01-27-2012, 05:46 AM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: 4 by 4 characters from String, right to left
There is a problem, using this:
last token, which length is less than 4, will not be considered.Java Code:while (binaryString.length() > 4) {
But I see the point, thanks for effort.
-
Re: 4 by 4 characters from String, right to left
Last edited by Fubarable; 01-27-2012 at 05:55 AM.
- 01-27-2012, 05:57 AM #8
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
- 01-30-2012, 08:03 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 08:56 AM #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.
dbJava 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); } }Why do they call it rush hour when nothing moves? - Robin Williams
- 01-30-2012, 05:48 PM #11
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: 4 by 4 characters from String, right to left
What does overkill means in this context?
- 01-30-2012, 06:45 PM #12
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
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 07:16 PM.
- 01-31-2012, 07:56 AM #13
Re: 4 by 4 characters from String, right to left
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,}.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
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 JSTLReplies: 1Last Post: 05-05-2011, 01:49 PM -
square moves left and down but not up or left
By natdizzle in forum AWT / SwingReplies: 3Last Post: 02-04-2011, 05:20 PM -
How to check whether the string contains only the specified characters ????
By j_kathiresan in forum New To JavaReplies: 1Last Post: 04-30-2010, 03:21 PM -
how to get the characters one by one from a String?
By Somitesh Chakraborty in forum New To JavaReplies: 3Last Post: 08-20-2008, 08:56 PM -
Getting all characters in a String
By Alayna in forum New To JavaReplies: 2Last Post: 05-20-2007, 11:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks