An example on splitting a string after 'n' characters i.e. into chunks with n characters each in Java.
And if we consider gowtham gutha whose length is an odd number (here 13) and the maximum size of each chunk be 2,then
Here, the no.of chunks are 7 and the last chunk size is just 1. If you can observe, the length of the string is an odd number and therefore we've got odd number of chunks.
i<k+1: Why? As said before not all the chunks length is the same as given length (len). Take a look at the above. The length of the last chunk is 1 (it contains only a).
m=0: m acts as cursor, it moves in accordance with the chunk i.e. if the size of the chunk is 2, initially it will be at starting of the string and later it jumps to 2 (0+2) and from 2 to 4 (2+2) and from 4 to 6 (4+2) and so on till the end of the string. All this happens when the length of the string is greater than the size of the chunk. If not, then the cursor will not move, it will be at 0 only and the chunk is the string only, so the string will be printed from 0 till the end.
if(i!=0) m=m+len: You'll have to split the string from starting. The initial value of m is 0 and it should not update for the first time, because if you can see the statement below, st.substring(m,m+len) for the first time, it has to be substring(0,0+len) for example, if len=2, then
substring(0,0+2)
So, the string has to split from starting (from index 0), that's why.
st.length()>m+len: If the length of the string is greater than the length of the sum of the cursor position and length of the string, then..
st.substring(m,m+len): Pick the part of the string between cursor position and the next cursor position. The next cursor position will be nothing but the sum of the current cursor position (current index) and the given length of the chunk.
Let's Split
import java.util.*;
class SplitByLength
{
public static void main(String args[])
{
// Create a Scanner object for taking input from cmd
Scanner s=new Scanner(System.in);
// Read a line from the user, space is not ignored
String st=s.nextLine();
// Take the split length
int len=s.nextInt();
// No.of possible strings for that length
int k=(st.length()/len);
// Print it if you wish to
System.out.println(k);
// Initialize the variable m=0, m acts as cursor
int m=0;
// Loop no.of possible string times [See analysis]
for(int i=0;i<(k+1);i++)
{
// Increase m value if i is not equal to 0 or greater than 0
if(i!=0) m=m+len;
// If length of string is greater or = to cursor position+split length, then..
if(st.length()>=m+len)
{
// Print the part of the string b/w cursor position and split length number of chars after cursor position
System.out.println((st.substring(m,m+len)));
}
// If length of string is less than cursor position + split length,then..
else if(st.length()<m+len)
{
// Print the string from cursor position till end
System.out.println((st.substring(m,st.length())));
}
}
}
}
Analysis
Why nextLine(): Because it does not ignore space, try using s.next() instead of s.nextLine() and run the program, you'll have a clear understand about the difference.
int len: The length of each chunk (fragment of string).
int k=st.length()/len: The no.of possible chunks with equal size. For example, consider gowthamgutha as the string that is to be split and the maximum size of each chunk be 2 (len=2), then the possible chunks are
go wt ha mg ut ha
And if we consider gowtham gutha whose length is an odd number (here 13) and the maximum size of each chunk be 2,then
go wt ha m gu th a
Here, the no.of chunks are 7 and the last chunk size is just 1. If you can observe, the length of the string is an odd number and therefore we've got odd number of chunks.
i<k+1: Why? As said before not all the chunks length is the same as given length (len). Take a look at the above. The length of the last chunk is 1 (it contains only a).
m=0: m acts as cursor, it moves in accordance with the chunk i.e. if the size of the chunk is 2, initially it will be at starting of the string and later it jumps to 2 (0+2) and from 2 to 4 (2+2) and from 4 to 6 (4+2) and so on till the end of the string. All this happens when the length of the string is greater than the size of the chunk. If not, then the cursor will not move, it will be at 0 only and the chunk is the string only, so the string will be printed from 0 till the end.
if(i!=0) m=m+len: You'll have to split the string from starting. The initial value of m is 0 and it should not update for the first time, because if you can see the statement below, st.substring(m,m+len) for the first time, it has to be substring(0,0+len) for example, if len=2, then
substring(0,0+2)
So, the string has to split from starting (from index 0), that's why.
st.length()>m+len: If the length of the string is greater than the length of the sum of the cursor position and length of the string, then..
st.substring(m,m+len): Pick the part of the string between cursor position and the next cursor position. The next cursor position will be nothing but the sum of the current cursor position (current index) and the given length of the chunk.
Test Cases
1.
gowthamgutha 2 6 6 go wt ha mg ut ha
2.
gowtham gutha 2 6 6 go wt ha m gu th a
3.
gowthamgutha 3 4 4 gow tha mgu tha
4.
gowtham gutha 3 4 4 gow tha m g uth a
No comments:
Post a Comment