Skip to main content
Inspiring
May 18, 2007
Answered

chopping text off a string

  • May 18, 2007
  • 2 replies
  • 283 views
&theVariable= I want to be able to eliminate the first 13 characters of this string so that I just have these two sentences. Is there a way to identify the first n chars, then , set the string equal to what's left over?

This topic has been closed for replies.
Correct answer
Taken from AS3 Manual:

Finding a substring by character position

The substr() and substring() methods are similar. Both return a substring of a string.
Both take two parameters. In both methods, the first parameter is the position of the starting
character in the given string. However, in the substr() method, the second parameter is the
length of the substring to return, and in the substring() method, the second parameter is
the position of the character at the end of the substring (which is not included in the returned
string). This example shows the difference between these two methods:

var str:String = "Hello from Paris, Texas!!!";
trace(str.substr(11,15)); // Paris, Texas!!!
trace(str.substring(11,15)); // output: Pari
Finding substrings and patterns in strings 215

The slice() method functions similarly to the substring() method. When given two nonnegative
integers as parameters, it works exactly the same. However, the slice() method can
take negative integers as parameters, in which case the character position is taken from the end
of the string, as shown in the following example:

var str:String = "Hello from Paris, Texas!!!";
trace(str.slice(11,15)); // Pari
trace(str.slice(-3,-1)); // !!
trace(str.slice(-3,26)); // !!!
trace(str.slice(-3,str.length)); // !!!
trace(str.slice(-8,-3)); // Texas

You can combine non-negative and negative integers as the parameters of the slice()
method.
Finding the character position of a matching
substring
You can use the indexOf() and lastIndexOf() methods to locate matching substrings
within a string, as the following example shows:

var str:String = "The moon, the stars, the sea, the land";
trace(str.indexOf("the")); // 10

Notice that the indexOf() method is case-sensitive.
You can specify a second parameter to indicate the index position in the string from which to
start the search, as follows:

var str:String = "The moon, the stars, the sea, the land"
trace(str.indexOf("the", 11)); // 21
The lastIndexOf() method finds the last occurrence of a substring in the string:
var str:String = "The moon, the stars, the sea, the land"
trace(str.lastIndexOf("the")); // 30

If you include a second parameter with the lastIndexOf() method, the search is conducted
from that index position in the string working backward (from right to left):

var str:String = "The moon, the stars, the sea, the land"
trace(str.lastIndexOf("the", 29)); // 21

2 replies

Correct answer
May 18, 2007
Taken from AS3 Manual:

Finding a substring by character position

The substr() and substring() methods are similar. Both return a substring of a string.
Both take two parameters. In both methods, the first parameter is the position of the starting
character in the given string. However, in the substr() method, the second parameter is the
length of the substring to return, and in the substring() method, the second parameter is
the position of the character at the end of the substring (which is not included in the returned
string). This example shows the difference between these two methods:

var str:String = "Hello from Paris, Texas!!!";
trace(str.substr(11,15)); // Paris, Texas!!!
trace(str.substring(11,15)); // output: Pari
Finding substrings and patterns in strings 215

The slice() method functions similarly to the substring() method. When given two nonnegative
integers as parameters, it works exactly the same. However, the slice() method can
take negative integers as parameters, in which case the character position is taken from the end
of the string, as shown in the following example:

var str:String = "Hello from Paris, Texas!!!";
trace(str.slice(11,15)); // Pari
trace(str.slice(-3,-1)); // !!
trace(str.slice(-3,26)); // !!!
trace(str.slice(-3,str.length)); // !!!
trace(str.slice(-8,-3)); // Texas

You can combine non-negative and negative integers as the parameters of the slice()
method.
Finding the character position of a matching
substring
You can use the indexOf() and lastIndexOf() methods to locate matching substrings
within a string, as the following example shows:

var str:String = "The moon, the stars, the sea, the land";
trace(str.indexOf("the")); // 10

Notice that the indexOf() method is case-sensitive.
You can specify a second parameter to indicate the index position in the string from which to
start the search, as follows:

var str:String = "The moon, the stars, the sea, the land"
trace(str.indexOf("the", 11)); // 21
The lastIndexOf() method finds the last occurrence of a substring in the string:
var str:String = "The moon, the stars, the sea, the land"
trace(str.lastIndexOf("the")); // 30

If you include a second parameter with the lastIndexOf() method, the search is conducted
from that index position in the string working backward (from right to left):

var str:String = "The moon, the stars, the sea, the land"
trace(str.lastIndexOf("the", 29)); // 21
kglad
Community Expert
Community Expert
May 18, 2007
yes, the flash string methods should be able to handle whatever it is that you want to do. i'm not sure what you want to do, but i'm sure the methods are adequate for the task.

in particular, the substr() and/or substring() methods should be helpful.