0

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kishore Kumar Korada
  • 1,204
  • 6
  • 22
  • 47
  • 1
    Of course you need to check the length before you substring. It is not clear to me what you're really asking, or what you're having problems with. – Mark Rotteveel Apr 04 '21 at 07:44
  • If the size of chunks which you want to extract is fixed, then you can do this stuff in simple `while` loop and check the length should be greater or equal to the chunk size, before performing substring operation. – Jignesh M. Khatri Apr 04 '21 at 07:47
  • @MarkRotteveel The problem is, we've been processing the string of an each index of an array. So, On that string, performing several substring operations where I know sub string indexes. But the problem is, sometimes, it's giving StringIndexOutofRange Exception which is because, some times, string length is not satisified – Kishore Kumar Korada Apr 04 '21 at 07:53
  • 2
    That doesn't help me understand your real problem, please provide a [mre] that actually demonstrates the problem. – Mark Rotteveel Apr 04 '21 at 08:02

2 Answers2

1

As I understand you need something like that. I hope this helps you:

String str = "abcdefghijklmnopqrstuvwxyz0123456789";
Map<String, String> values = new HashMap<>();
int count = 0;

while (str.length() > 20) {
    values.put("key" + count, str.substring(0, 21));
    str = str.substring(21);
    count++;
}

values.put("key" + count, str);
JAGC
  • 68
  • 8
0

Write a function to abstract the rules you need to follow when extracting a value:

void extractValue(Map<String, String> values, String source, String key, int valueStart, int valueEnd) {
        if (source.length() >= valueEnd) {
            values.put(key, source.substring(valueStart, valueEnd));
        }
    }

(I don't understand your rules for when you can extract, so this is just and example of what you might do)

tgdavies
  • 10,307
  • 4
  • 35
  • 40