• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Split a string into multiple lines based on length

Engaged ,
Oct 27, 2017 Oct 27, 2017

Copy link to clipboard

Copied

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.name == "coverPageDecriptionLineOne") {

                        if (getText(titleDescription).length < 27){

                            alert("single line");

                            }

                        if (getText(titleDescription).length > 27 && getText(titleDescription).length < 54){

                            alert("two line");

                            }

                        if (getText(titleDescription).length > 54 && getText(titleDescription).length < 81){

                            alert("three line");

                            }

                        if (getText(titleDescription).length > 81 && getText(titleDescription).length < 108){

                            alert("four line");

                            }

                        if (getText(titleDescription).length > 108){

                            alert("that's a long freakin description");

                            }

                        alert("Your title is " + getText(titleDescription).length + " characters in it");

                        }

TOPICS
Scripting

Views

4.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 30, 2017 Oct 30, 2017

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.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("

...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 27, 2017 Oct 27, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 27, 2017 Oct 27, 2017

Copy link to clipboard

Copied

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"

Thank you for your help! williamadowling​

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2017 Oct 27, 2017

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 27, 2017 Oct 27, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2017 Oct 27, 2017

Copy link to clipboard

Copied

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;

    alert("Your title has " + inputLen + " characters in it.");

    if(inputLen > 108)

    {

        alert("that's a long freakin description");

    }

    for(var x=0;x<inputArrayLen;x++)

    {

        if(curLen + inputArray.length < LINE_LENGTH)

        {

            formattedStr += " " + inputArray;

            curLen += inputArray.length + 1;

        }

        else

        {

            curLen = 0;

            formattedStr += "\n" + inputArray;

            curLen += inputArray.length;

        }

    }

    return formattedStr;

}

for (z = 0; z < theDocVariables.length; z++) { 

    // Start finding variables here 

    if (theDocVariables.name == "coverPageDecriptionLineOne") { 

        var userInput = getText(titleDescription);

        var formattedOutput = formatDesc(userInput);

        alert("result =\n" + formattedOutput);

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 30, 2017 Oct 30, 2017

Copy link to clipboard

Copied

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.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;

    alert("Your title has " + inputLen + " characters in it.");

    if(inputLen > 108)

    {

        alert("that's a long freakin description");

    }

    for(var x=0;x<inputArrayLen;x++)

    {

        if(curLen + inputArray.length < LINE_LENGTH)

        {

            formattedStr += " " + inputArray;

            curLen += inputArray.length + 1;

        }

        else

        {

            formattedStr += "\n" + inputArray;

            curLen = inputArray.length;

        }

    }

    return formattedStr;

}

for (z = 0; z < theDocVariables.length; z++) { 

    // Start finding variables here 

    if (theDocVariables.name == "coverPageDecriptionLineOne") { 

        var userInput = getText(titleDescription);

        var formattedOutput = formatDesc(userInput);

        alert("result =\n" + formattedOutput);

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 01, 2017 Nov 01, 2017

Copy link to clipboard

Copied

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.name == "coverPageDecriptionLineOne") {   

        var userInput = getText(titleDescription); 

        var formattedOutput = formatDesc(userInput);

        theDocVariables.pageItems[0].contents = formattedOutput;

        alert("result =\n" + 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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2017 Nov 01, 2017

Copy link to clipboard

Copied

LATEST

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; 

 

 

    alert("Your title has " + inputLen + " characters in it."); 

    if(inputLen > 108) 

    { 

        alert("that's a long freakin description"); 

    } 

    for(var x=0;x<inputArrayLen;x++) 

    { 

        if(curLen + inputArray.length < LINE_LENGTH) 

        { 

            formattedStr += " " + inputArray

            curLen += inputArray.length + 1; 

        } 

        else 

        { 

            formattedStr += "\n" + inputArray

            curLen = inputArray.length; 

        } 

    } 

    return formattedStr; 

}  

 

 

for (z = 0; z < theDocVariables.length; z++) {   

    // Start finding variables here   

    if (theDocVariables.name == "coverPageDecriptionLineOne") {   

        var userInput = getText(titleDescription); 

        var formattedOutput = formatDesc(userInput);

        var coverPageDescription = {};

        alert("result =\n" + formattedOutput);

        var formattedLines = formattedOutput.split("\n");

        var descLen = formattedLines.length;

        for(var x=0;x<descLen;x++)

        {

            coverPageDescription["line " + x] = formattedLines;

        }

    } 

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines