Reputation: 55
I'm taking an intro prog class so I am only allowed to use methods of string objects. There is probably other 'prettier' way to solve it but I'm not allowed to use more advanced techniques. Pretend that I have a string "abcdefghijklmnopqrstuvwxyz" I want to split it into different parts of equal length. My attempted solution is below which does not work. It only prints out one part, in this case, "aaa." I'm not sure which string method is the best. I figured substring which be the way to go but not sure on how to implement it correctly.
int splitSize = 5
String s = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooo";
for (int i = 0; i < s.length() / splitSize; i++) {
s = s.substring(i, i + 3);
}
Upvotes: 0
Views: 1039
Reputation: 20526
Another way is to use String's charAt
method.
int splitSize = 5;
String s = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooo";
for (int i = 0; i < s.length(); i++) {
System.out.print(s.charAt(i));
if ((i + 1) % splitSize == 0) {
System.out.print("\n");
}
}
Each character in the string is printed to the console and when the index i
is divisible with the splitSize
, it starts a new line.
Upvotes: 0
Reputation: 17945
Let us assume you want your output as an array of strings (you are currently ignoring output; another option would be to print it). Then you can implement something like
public static String[] split(String s, int splitSize) {
String[] result = new String[(s.length()+splitSize-1)/splitSize];
for (int i=0, j=0; i<s.length(); i+=splitSize) {
result[j++] = s.substring(i, Math.min(s.length(), i+splitSize));
}
return result;
}
This will return an array with consecutive substrings of splitSize
characters each, where the last one may be smaller than the rest if it could not be filled in. If you need to even them out, there's something called Bresenham's algorithm that does just that (although it is more commonly used for drawing lines)
Notice that "1".substring(0,3)
throws an exception - you should not try to take a larger substring than is available.
You can avoid using Math.min
when adding to the result by either using an if
statement or the ternary operator (?:
).
Upvotes: 1
Reputation: 4458
In your for loop, you are trying to divide String(s) by an integer(splitSize). It will give you a compile error.
You can try something like this:
static void split(int splitSize) {
String s = "aaaaabbbbbccccc";
for (int i = 0; i < s.length(); i += splitSize) {
System.out.println(s.substring(i, i+splitSize));
}
}
Prints:
aaaaa
bbbbb
ccccc
If that's what you are looking for. Hope that helps.
Upvotes: 0
Reputation: 206
You should use "count/flag" to keep an eye on string index and split size. check out this code
int splitSize = 5
String s = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooo";
int count = 0;
String tempString = "";
for (int i = 0; i < s.length(); i++) {
String str_atIndex = Character.toString(s.charAt(i));
if(count < splitSize){
tempString = tempString + str_atIndex;
count++;
}else{
count = 0;
System.out.print(tempString);
tempString = "";
}
}
Upvotes: 0
Reputation: 771
Try this (s.length()
must be a multiple of splitSize
).
int splitSize = 5;
String s = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooo";
for (int i = 0; i < s.length();) {
System.out.println(s.substring(i, i + splitSize));
i = i + splitSize;
}
Upvotes: 0