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

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

Guest
Oct 21, 2015 Oct 21, 2015

Copy link to clipboard

Copied

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

TOPICS
ActionScript

Views

596

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 21, 2015 Oct 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;

Votes

Translate

Translate
Enthusiast ,
Oct 21, 2015 Oct 21, 2015

Copy link to clipboard

Copied

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

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 21, 2015 Oct 21, 2015

Copy link to clipboard

Copied

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

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 21, 2015 Oct 21, 2015

Copy link to clipboard

Copied

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;

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
Guest
Oct 21, 2015 Oct 21, 2015

Copy link to clipboard

Copied

LATEST

Cool, these answers are fast !!

Thank you for your help, it works like i want

But i have changed a part of your script, this :

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


because it repeat the last letter, at the beginning of the next line, for example :

Demain il fau

udra aller re

endre le livr

re.


And when i put this :

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


It's ok

Demain il fau

dra aller ren

dre le livre.

Bye

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 21, 2015 Oct 21, 2015

Copy link to clipboard

Copied

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');

}

}

}

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