Skip to main content
October 21, 2015
Answered

How to split a text in a textbox, by length?

  • October 21, 2015
  • 3 replies
  • 791 views

Hello and sorry for my English if a make mistake , i would like to cut a text with a length parameter, and not a special characters.

For example, i have this text :

Aujourd'hui, je suis allé chercher un livre.


And i would like have this sentence (with a parameter of "13" (for 13 characters for example)), when i click on a button :

Aujourd'hui j

e suis allé c

hercher un li

vre.

Peharps an actionscript3 like this logic:

text1.text = (line1.length = 13) + /n + line2.length = 13) + /n...

There's not a special characters, just the length because the text is unknow...

Thanks for your help

I continue to looking for a solution

This topic has been closed for replies.
Correct answer Joseph Labrecque

Oops. Didn't instantiate the String. Fixed:

var s:String = "Aujourd'hui, je suis allé chercher un livre.";

var c:int = s.length;

var split:int = 13;

var n:String = "";

for(var i:int=0; i<c; i+=split){

  n += s.substr(i, split+1) + "\n";

}

trace(n);

myText.text = n;

3 replies

kglad
Community Expert
Community Expert
October 21, 2015

and here's a more general solution:

function splitF(tf:TextField,s:String,n:int):void{

var a:Array = s.split('');

tf.multiline = true;

tf.text = a[0];

for(var i:int=1;i<a.length;i++){

tf.appendText(a);

if(i%n==0){

tf.appendText('\n');

}

}

}

Joseph Labrecque
Community Expert
Community Expert
October 21, 2015

This should do it for you!

var s:String = "Aujourd'hui, je suis allé chercher un livre.";

var c:int = s.length;

var split:int = 13;

var n:String;

for(var i:int=0; i<c; i+=split){

  n += s.substr(i, split+1) + "\n";

}

trace(n);

Joseph Labrecque
Community Expert
Joseph LabrecqueCommunity ExpertCorrect answer
Community Expert
October 21, 2015

Oops. Didn't instantiate the String. Fixed:

var s:String = "Aujourd'hui, je suis allé chercher un livre.";

var c:int = s.length;

var split:int = 13;

var n:String = "";

for(var i:int=0; i<c; i+=split){

  n += s.substr(i, split+1) + "\n";

}

trace(n);

myText.text = n;

Inspiring
October 21, 2015

There's no property to do that, you can use line break in the string:

var _text:String = "Aujourd'hui, j" + "\n" + "e suis allé c" + "\n" + "hercher un li" + "\n" + "vre.";

Or:

textFieldName.text = "Aujourd'hui, j" + "\n" + "e suis allé c" + "\n" + "hercher un li" + "\n" + "vre.";