I have to break up a string field into multiple string fields if the original string exceeds a specific character limit. The original string can be of different lengths but the additional fields like string2, and string3 have a max character length of 10.
Question:
- How can I break up a single string field into multiple string fields with defined character limits based on the nearest whitespace to the character limit?
Example:
Hello world, how are you doing?
Assumptions:
originalStringcan be of different string lengthsstring1is maxed at 15 charactersstring2is maxed at 10 charactersstring3is maxed at 10 characters- first 15 characters for the
originalStringwould beHello world, ho - next 10 characters would be
w are you - next 10 character would be
doing? - Do not break up whole words
- only populate street2 and street3 if required. if all characters can fit into street1 and street2 then street3 can be empty
What I tried that didn't work:
let originalString = `Hello world, how are you doing?`
if(originalString.length > 15) {
let string1 = originalString.substring(0,15) // this returns `Hello World,`
let string2 = ???? // stuck here - need help
let string3 = ???? // stuck here - need help
}
Expected output:
originalString=Hello world, how are you doing?string1=Hello world,string2=how arestring3=you doing?