0

I want to split text when length is 30 including space. My work so far:

var m = "Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co";

var spacedM = m.split(' ');
var charCount = 0;

for(var i = 0; i < spacedM.length; i++){

    charCount = charCount + spacedM[i].length + 0.5; 

if(charCount <= 30 && $('#address1').text().length <= 30){
    $('#address1').append(spacedM[i]+' ');
} else if(charCount > 30 && charCount <= 60 && $('#address2').text().length <= 30) {
    $('#address2').append(spacedM[i]+' ');
} else if(charCount > 60 && charCount <= 90 && $('#address3').text().length <= 30) {
        $('#address3').append(spacedM[i]+' ');
}

}

$('#address1').append($('#address1').text().length);
$('#address2').append($('#address2').text().length);
$('#address3').append($('#address3').text().length);

//output
Lorem ipsum dolor sit amet, co 31
Lorem ipsum dolor sit amet, co 31
Lorem ipsum dolor sit amet, co 31

It look like ok. But it kind of a hack too. Isn't it?. I welcome any suggestion to improve this solution. Since this code will be used to split address for older data to map it inside 3 fields of address. Below is my jsfiddle: https://jsfiddle.net/u11p6xx4/4/

UPDATED: I do not want split words. Because word in address can't split to 2 part if they are meant for 1 word. So it is actually splitting address when chars are less than 30 but don't split word. The chars can be 28 in length and then continue in #address2

Example address : Blok 53-11-04 Apartment Flamingo, Keramat Jaya 2 Persiaran Gurney

Expected :

Blok 53-11-04 Apartment
Flamingo, Keramat Jaya 2
Persiaran Gurney
7
  • You can just do yourString.match(/.{30}/g)... Commented Apr 5, 2017 at 2:27
  • Do you want to keep no more than 30 chars, but it should be whole words (not cutted), right ? Commented Apr 5, 2017 at 2:27
  • @Derek朕會功夫, better to use string.substr(0, 30) then Commented Apr 5, 2017 at 2:29
  • @AlexandrKapustin Well OP is trying to split the string up in groups of 30 characters, or at least that's what I think he's trying to do. Commented Apr 5, 2017 at 2:30
  • I think your question would be clearer if the example data didn't have the same text repeated multiple times, so that the output wasn't just exactly the same string three times. Commented Apr 5, 2017 at 2:34

3 Answers 3

2

Why can't you just use regex? Like:

var m = "Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co";

var n = m.match(/.{31}/g);
$('#address1').append(n[0]);
$('#address2').append(n[1]);
$('#address3').append(n[2]);

$('#address1').append($('#address1').text().length);
$('#address2').append($('#address2').text().length);
$('#address3').append($('#address3').text().length);

// output
// Lorem ipsum dolor sit amet, co 31
// Lorem ipsum dolor sit amet, co 31
// Lorem ipsum dolor sit amet, co 31

But what happens if there is a 4th group matched? Do you just ignore everything from the ((31*3)+1)-th character?

Update:

Try using this regex /[^\W].{1,30}(?:\s|$)/g, you'll still need to improve it but it should get you started:

var m = "Blok 53-11-04 Apartment Flamingo, Keramat Jaya 2 Persiaran Gurney";

var n = m.match(/.{1,30}(?:\s|$)/g); // or /[^\W].{1,30}(?:\s|$)/g

$('#address1').append(n[0]);
$('#address2').append(n[1]);
$('#address3').append(n[2]);

// output
// Blok 53-11-04 Apartment
// Flamingo, Keramat Jaya 2
// Persiaran Gurney

You can expirement here: https://regex101.com/r/TIRa6L/2

If you wan't a more reliable approach try a so called "address verification api". Something like:

It should be able to parse a 1 line address and convert it into the correct multi-line format.

Sign up to request clarification or add additional context in comments.

6 Comments

so if we change m to: var m = " Lorem i.... we will get Lorem ipsum dolor sit amet, co L31 I do not think it's expected :D
@AlexandrKapustin, I'll leave it to the OP to fix. It's just some condition and stripping.
i just update my question. Actually i do not want to split 1 word into to word. Chars can be less than 30, but keep word without splitting.
@AziziMusa I think you should provide a real address example and a real expected result.
owh my goodness. Regex is powerful. I still scratching my head in jsfiddle using javascript. Your solution is right. But let me testing with several address.
|
0

Using a For loop like your original post. Not sure what your requirements are as far as truncation goes though. This snippet does not care about truncating words. It just splits at every 30 chars.

<!-- goal is to split text when length is 30 including space -->
var m = "Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co";

var spacedM = m.split('');
var charCount = 0;
var theString = "";
var theStrings = [];
for(var b=0; b < spacedM.length; b++)
{
    theString = theString + spacedM[b];
    if(charCount == 29)
    {
        theStrings.push(theString);//add this string to the array of strings
        theString = "";//reset theString
        charCount = 0;//reset the charCount
    }
    charCount++;//increment the charCount
}

for(var i=0; i < theStrings.length ;i++)
{
    console.log(theStrings[i]);
}

Comments

0

May can use regexp to match it. https://regex101.com/r/IszFAZ/1

And it can support the last word with any length.

var m = "Lorem ipsum dolor sit amet, c1 Lorem ipsum dolor sit amet, co2 Lorem ipsum dolor sit amet, coo3 Lorem ipsum dolor sit amet, c4";
console.log(m.match(/(?!\s).{30,}?(?=\s|$)/g));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.