I've a string with uncertain length. Let's say
I've the below string with length of 199. Index from 0 to 198.
string str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type";
And here I perform substring operations to extract content. It looks like this
Map<String,String> values = new HashMap<>();
values.put("key1",str.subString(0,20));
values.put("key2",str.subString(20,55));
...
...
values.put("key3",str.subString(172,198));
And and above code works as I know the length. But sometimes, the length is uncertain to me. And in those situations, How do I validate the content? Specifically should I use if condition for every substring? If so, should I add it like
if(str.length() >= 20 && str.length < 55) {
values.put("key1",str.subString(0,20));
}
if(str.length() >= 55 && str.length < 70) {
values.put("key2",str.subString(20,55);
}
..
..
if(str.length() >= 198) {
values.put("key3",str.subString(172,197));
}
But here the problem is, String length might change (it could be for array of strings with different lengths) and some of the content might not be there. And I get IndexOutOfRange exception with the conditions. So, what would you suggest? Any good practice I need to follow?