Considering the following string, I would like to get a substring in that way:
String str="abcdefghij"
Substring returned:
acegi
that is to say every two chars of the first string str.
Then with the same reasoning, getting a substring of every three chars of the first string str.
And the same question with a frequency of f
This is my current method but actually there is an error:
private static String subString(String str,int freq){
String subStr=null;
for(int i = 0; i < str.length(); freq++){
subStr= subStr + str[i];
}
return subStr;
}
Also maybe there is some already existing method for these tasks?
I don’t know of a built-in method to do it. You could use a StringBuilder to avoid creating many temporary strings, and initialise it with the right capacity: