8 Replies Latest reply on Nov 1, 2017 9:55 AM by williamadowling

# Split a string into multiple lines based on length

I am allowing the user to input a description. What I am needing is to split up the string they input based on 27 characters.

So if their description is less than 27 characters....

It is a single string and needs to alert them of that.

If their description is more than 27 but less than 54 characters....

I need it to find the space closest to the 27th character, then split the string into 2 lines

If their description is more than 54 but less than 81 characters....

I need it to find the space closest to the 54th character, then split the string into 3 lines

If their description is more than 81 but less than 108 characters....

I need it to find the space closest to the 81st character, then split the string into 4 lines

If their description is more than 108 characters....

Alert that it is really long

Here is a section of code I am working with

```                for (z = 0; z < theDocVariables.length; z++) {
// Start finding variables here
if (theDocVariables[z].name == "coverPageDecriptionLineOne") {

if (getText(titleDescription).length < 27){
}
if (getText(titleDescription).length > 27 && getText(titleDescription).length < 54){
}
if (getText(titleDescription).length > 54 && getText(titleDescription).length < 81){
}
if (getText(titleDescription).length > 81 && getText(titleDescription).length < 108){
}
if (getText(titleDescription).length > 108){
}
}
```
• ###### 1. Re: Split a string into multiple lines based on length

Can you share the getText() function? Is that returning a string? What logic does it use to determine which part of a string to return?

Are you trying to split the string into lines that are all ~27 characters? so if the length is between 54 and 81, you want 3 lines of approximately 27?

Are you looking for the closest space in either direction from the 27 character mark? Or once you reach 27 characters, you want to split the string at the next space?

I believe i have something working, but i want to make sure i have all the parameters correct for your needs.

• ###### 2. Re: Split a string into multiple lines based on length

Here is the getText()

```// Function to collect user input
function getText(field) {
return field.text !== undefined ? field.text : "";
}
```

It is returning a string from a user input.

The string should be split at the space closest to 27 characters. So if the user types in...

I am typing in a title description here so we can get it formatted

27 characters is.... I am typing in a title desc

So I need it to cut the string at the space before the word description (so the word doesn't get cut in half).

Then the 27 characters would be.....description here so we can (since we have dropped the word description to a new string...the count would start again).

So the final output would be

stringOne = "I am typing in a title"

stringTwo = "description here so we can"

stringThree = "get it formatted"

• ###### 3. Re: Split a string into multiple lines based on length

So do you always want the lines to be LESS than 27 characters? Or just as close to 27 as possible without splitting any words?

• ###### 4. Re: Split a string into multiple lines based on length

yes always less than 27 characters and yes without splitting any words

• ###### 5. Re: Split a string into multiple lines based on length

This works for the test case you provided. Though it may take some reworking so that you can integrate it into your existing code. Plus i would run it through a robust set of test cases to make sure it does what you expect.

```function formatDesc(str)
{
var inputLen = str.length,
inputArray = str.split(" "),
inputArrayLen = inputArray.length;
formattedStr = "",
curLen = 0,
LINE_LENGTH = 27;

if(inputLen > 108)
{
}
for(var x=0;x<inputArrayLen;x++)
{
if(curLen + inputArray[x].length < LINE_LENGTH)
{
formattedStr += " " + inputArray[x];
curLen += inputArray[x].length + 1;
}
else
{
curLen = 0;
formattedStr += "\n" + inputArray[x];
curLen += inputArray[x].length;
}
}
return formattedStr;
}

for (z = 0; z < theDocVariables.length; z++) {
// Start finding variables here
if (theDocVariables[z].name == "coverPageDecriptionLineOne") {
var userInput = getText(titleDescription);
var formattedOutput = formatDesc(userInput);
}
}
```
• ###### 6. Re: Split a string into multiple lines based on length

woops. i just noticed a small typo. it shouldn't break anything but it's going to create some global variables unnecessarily. on line 5 i used a semi colon instead of a comma.

Also, i realized that i had some redundancy in the else statement.. i could have left out the "curLen = 0" and simply used "curLen = inputArray[x].length".

Oh well. Always a work in progress. Hope this works for you.

here's the fixed version:

```function formatDesc(str)
{
var inputLen = str.length,
inputArray = str.split(" "),
inputArrayLen = inputArray.length,
formattedStr = "",
curLen = 0,
LINE_LENGTH = 27;

if(inputLen > 108)
{
}
for(var x=0;x<inputArrayLen;x++)
{
if(curLen + inputArray[x].length < LINE_LENGTH)
{
formattedStr += " " + inputArray[x];
curLen += inputArray[x].length + 1;
}
else
{
formattedStr += "\n" + inputArray[x];
curLen = inputArray[x].length;
}
}
return formattedStr;
}

for (z = 0; z < theDocVariables.length; z++) {
// Start finding variables here
if (theDocVariables[z].name == "coverPageDecriptionLineOne") {
var userInput = getText(titleDescription);
var formattedOutput = formatDesc(userInput);
}
}
```
• ###### 7. Re: Split a string into multiple lines based on length

williamadowling THANK YOU! this is exactly what I needed it to do!...

so I was wondering if you could help me with one more issue....

If the users input is a single line....

```    if (theDocVariables[z].name == "coverPageDecriptionLineOne") {
var userInput = getText(titleDescription);
var formattedOutput = formatDesc(userInput);
theDocVariables[z].pageItems[0].contents = formattedOutput;
}
```

Is there a way to make it so that.... if the users input is a single line (less than 27 characters).... the users input replaces the contents in variable coverPageDecriptionLineOne

If it is 2 lines.... it replaces the contents of the variables like this....

first line replaces coverPageDecriptionLineOne

second line replaces coverPageDecriptionLineTwo

If it is 3 lines.... it replaces the contents of the variables like this....

first line replaces coverPageDecriptionLineOne

second line replaces coverPageDecriptionLineTwo

third line replaces coverPageDecriptionLineThree

If it is 4 lines.... it replaces the contents of the variables like this....

first line replaces coverPageDecriptionLineOne

second line replaces coverPageDecriptionLineTwo

third line replaces coverPageDecriptionLineThree

fourth line replaces coverPageDecriptionLineFour

Hope that makes sense...again thank you so much for your solution!

• ###### 8. Re: Split a string into multiple lines based on length

hmm. the way i'd tackle that is to just split the formatted string by the newline character, then loop the resulting array to do whatever you were going to do with the variables you wanted to create. It can be tough to create an unknown number of variables conditionally.

You could declare a bunch of variables and leave them uninitialized.. Then define the necessary variables based on the condition.

In my opinion, it's generally not good practice to create variables "just in case you might need them later".

Here's my recommendation. declare an empty object literal called coverPageDescription split the text frame, loop the array and create a property for each line. try this:

```function formatDesc(str)
{
var inputLen = str.length,
inputArray = str.split(" "),
inputArrayLen = inputArray.length,
formattedStr = "",
curLen = 0,
LINE_LENGTH = 27;

if(inputLen > 108)
{
}
for(var x=0;x<inputArrayLen;x++)
{
if(curLen + inputArray[x].length < LINE_LENGTH)
{
formattedStr += " " + inputArray[x];
curLen += inputArray[x].length + 1;
}
else
{
formattedStr += "\n" + inputArray[x];
curLen = inputArray[x].length;
}
}
return formattedStr;
}

for (z = 0; z < theDocVariables.length; z++) {
// Start finding variables here
if (theDocVariables[z].name == "coverPageDecriptionLineOne") {
var userInput = getText(titleDescription);
var formattedOutput = formatDesc(userInput);
var coverPageDescription = {};