I have this piece of code in my java class
mystring = mysuperstring.split("/");
I want to know how many sub-string is created from the split.
In normal situation, if i want to access the first sub-string i just write
mystring[0];
Also, i want to know if mystring[5]
exist or not.
Total number of substrings = n + (n - 1) + (n - 2) + (n - 3) + (n - 4) + ……. + 2 + 1. So now we have a formula for evaluating the number of substrings where n is the length of a given string.
Approach: The count of sub-strings of length n will always be len – n + 1 where len is the length of the given string.
I want to know how many sub-string is created from the split.
Since mystring
is an array, you can simply use mystring.length
to get the number of substrings.
Also, i want to know if
mystring[5]
exist or not.
To do this:
if (mystring.length >= 6) { ... }
mystring = mysuperstring.split("/");
int size = mystring.length;
remember that arrays are zero indexed, so where length = 5, the last element will be indexed with 4.
It's a simple way to count the sub-strings
word.split('/').length;
You can see an example of this implementation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With