# String trouble

**URL:** https://forum.kirupa.com/t/string-trouble/23474
**Category:** flash
**Created:** [June 18, 2003, 1:09pm UTC](https://forum.kirupa.com/t/string-trouble/23474 "2003-06-18T13:09:28Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Stanley](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Stanley](https://forum.kirupa.com/u/Stanley)
#### Post date: [June 18, 2003, 1:09pm UTC](https://forum.kirupa.com/t/string-trouble/23474/1 "2003-06-18T13:09:28Z")

</div>

Hi everyone,

I use this code to create a short text of 90 characters and than add three dots.

text = \_root.shorttext1;  
short = new String(text);  
subshort = short.substr(0, 90)+" …";

Problem is that i don’t want to cut off the text in the middle of a word. How can I make sure flash never leaves half a word, but instead makes the string shorter so that it ends on a space character.

So instead of “word word word word word word word wo…”  
I want “word word word word word word word …”

thanks in advance

S

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 1:20pm UTC](https://forum.kirupa.com/t/string-trouble/23474/2 "2003-06-18T13:20:38Z")

</div>

i dont know if its possible, but i was thinking about his:

if you slice the string into an array, with the spaces being the cutters, so you have an array with all words in it

words[1] being the first word of your text etc etc

and then if you want to only view the first 10 words, and after that adding … to it, you should go with this:

```auto

for(i=1;1<=X;i++){
text += words*+" ";
}
text += " ...":

```

X being the number of words you want

should be something like that, all you need to know now is, how to slice an string to an array…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 1:29pm UTC](https://forum.kirupa.com/t/string-trouble/23474/3 "2003-06-18T13:29:04Z")

</div>

Problem is my design has only room for 90 chars. If I would allow the first 12 words, they could contain more than 90 letters…

the setup:  
Each small block of text is set next to a thumbnail. Then people can click the thumb and then get a larger image, accompanied by the whole text…

But anyway: thanks for trying !

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 1:31pm UTC](https://forum.kirupa.com/t/string-trouble/23474/4 "2003-06-18T13:31:59Z")

</div>

i found an answer, check back in 15 minutes

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 1:35pm UTC](https://forum.kirupa.com/t/string-trouble/23474/5 "2003-06-18T13:35:39Z")

</div>

wow man,  
that’s great.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 1:43pm UTC](https://forum.kirupa.com/t/string-trouble/23474/6 "2003-06-18T13:43:00Z")

</div>

\*\* READ NEXT POST \*\*

```auto

// this is the text
myText = "Problem is that i don't want to cut off the text in the middle of a word. How can I make sure flash never leaves half a word, but instead makes the string shorter so that it ends on a space character.";
// slice it back to 90 characters
newText = myText.substr(0,90);
// put thos characters in an array
Words = newText.split(" ",99);
//loop tru the array, not including the last word
for(i=0;i<Words.length-1;i++){
	// put the word including a space into a new var
	shortText += Words*+" ";
}
// add 3 dots at the end
shortText += "...";
//and there you have it
trace(shortText)

```

there ya go 😃

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 1:46pm UTC](https://forum.kirupa.com/t/string-trouble/23474/7 "2003-06-18T13:46:38Z")

</div>

this is the correct code:

```auto

// this is the text
myText = "Problem is that i don't want to cut off the text in the middle of a word. How can I make sure flash never leaves half a word, but instead makes the string shorter so that it ends on a space character.";
// slice it back to 90 characters
newText = myText.substr(0,87);
// put thos characters in an array
Words = newText.split(" ",99);
//loop tru the array, not including the last word
for(i=0;i<Words.length-1;i++){
        // put the word including a space into a new var
        shortText += Words*+" ";
}
// add 3 dots at the end
shortText += "...";
//and there you have it
trace(shortText)

```

this one will always return 90 characters, the previeus one, would return 93… 🙂 (the 3 dots ;))

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 2:05pm UTC](https://forum.kirupa.com/t/string-trouble/23474/8 "2003-06-18T14:05:14Z")

</div>

Looks christalclear to me. awesome man…  
I owe you one

stanley

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 2:27pm UTC](https://forum.kirupa.com/t/string-trouble/23474/9 "2003-06-18T14:27:19Z")

</div>

just send me 200 euro’s to this back #: 112561616 😛

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 3:03pm UTC](https://forum.kirupa.com/t/string-trouble/23474/10 "2003-06-18T15:03:37Z")

</div>

makeList = function(){  
for(i=2;i\<=\_root.projectcount;i++) {  
duplicateMovieClip(“serviceunit”, “serviceunit”+i, i);  
unit\_i = eval(“serviceunit”+i);  
unit\_i.id = eval("\_root.id"+i);  
unit\_i.\_y = [code to determine \_y value]  
unit\_i.subclip.title = eval("\_root.title"+i);

```
            _root.maxdepth = unit_i.getDepth();

           //the string part ... 
  myText = eval("_root.shorttext"+i);
           // slice it back to 90 characters
  newText = myText.substr(0,87);
           // put thos characters in an array
  Words = newText.split(" ",99);
          //loop tru the array, not including the last word
	for(t=0;t&lt;Words.length-1;t++){               

```

// put the word including a space into a new var  
subshort += Words[t]+" ";  
}  
// add 3 dots at the end  
subshort += “…”;  
//

```
          unit_i.subclip.shorttext = subshort;
          unit_i.subclip.thumbcontainer.loadMovie("portfolio/thumb_"+unit_i.id+".jpg");
			
} 
}

```

makeList();

* * *

Dammit. I’m not in the clear yet :hangover: There’s a conflict going on here. With the code above, every duplicated (thumbnail-) movieclip gets the same tekst.  
All the original texts are on the root and are named like this: \_root.shorttext1  
\_root.shorttext2  
\_root.shorttext3  
(dynamically loaded from an external file)

Am I missing something here?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 3:05pm UTC](https://forum.kirupa.com/t/string-trouble/23474/11 "2003-06-18T15:05:56Z")

</div>

omg, this is really messy

```auto

makeList = function () {
	for (i=2; i<=_root.projectcount; i++) {
		duplicateMovieClip("serviceunit", "serviceunit"+i, i);
		unit_i = eval("serviceunit"+i);
		unit_i.id = eval("_root.id"+i);
		unit_i._y = 20; // edit this
		unit_i.subclip.title = eval("_root.title"+i);
		_root.maxdepth = unit_i.getDepth();
		// this is the text
		myText = eval("_root.shorttext"+i);
		// slice it back to 90 characters
		newText = myText.substr(0, 87);
		// put thos characters in an array
		Words = newText.split(" ", 99);
		// loop tru the array, not including the last word
		for (i=0; i<Words.length-1; i++) {
			// put the word including a space into a new var
			subshort += Words*+" ";
		}
		// add 3 dots at the end
		subshort += "...";
		// and there you have it
		unit_i.subclip.shorttext = subshort;
		unit_i.subclip.thumbcontainer.loadMovie("portfolio/thumb_"+unit_i.id+".jpg");
	}
};
makeList();

```

this is without errors

and to make it more clear, here’s an function to cut the text

```auto

cutText = function (x) {
	myText = x;
	newText = myText.substr(0, 87);
	Words = newText.split(" ", 99);
	for (i=0; i<Words.length-1; i++) {
		subshort += Words*+" ";
	}
	subshort += "...";
	return subshort
};
makeList = function () {
	for (i=2; i<=_root.projectcount; i++) {
		duplicateMovieClip("serviceunit", "serviceunit"+i, i);
		unit_i = eval("serviceunit"+i);
		unit_i.id = eval("_root.id"+i);
		unit_i._y = 20;
		unit_i.subclip.title = eval("_root.title"+i);
		_root.maxdepth = unit_i.getDepth();
		unit_i.subclip.shorttext = cutText(eval("_root.shorttext"+i));
		unit_i.subclip.thumbcontainer.loadMovie("portfolio/thumb_"+unit_i.id+".jpg");
	}
};
makeList();

```

including ur script

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 18, 2003, 3:20pm UTC](https://forum.kirupa.com/t/string-trouble/23474/12 "2003-06-18T15:20:09Z")

</div>

It’ll take me some time to make txtfiles to replace database information and clean up the fla a little. It’s also part of another movie. You’ll almost need the whole site or you’ll see nothing…

I’ll get back with this…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 20, 2003, 9:22am UTC](https://forum.kirupa.com/t/string-trouble/23474/13 "2003-06-20T09:22:08Z")

</div>

Wow…  
again way much more than what i bargained for 🙂  
you rock !  
thanks

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 20, 2003, 9:57am UTC](https://forum.kirupa.com/t/string-trouble/23474/14 "2003-06-20T09:57:57Z")

</div>

np dude… its just an hobby
