Skill Sets:

Home

  1. Intro to Flash
  2. User Input
  3. Fancy Controls 1
  4. Control Flow: if
  5. Control Flow: loop
  6. Nested Structures
  7. Graphics 1
  8. Arrays
  9. Strings
  10. Mouse Movement
  11. Functions
  12. Grouping Images
  13. Graphical Support
  14. Animation
  15. Physics
  16. 2010 Grade 10 Summative showcase
  17. 2010 Grade 11 Summatives
  18. 2011 Grade 10 Summatives
  19. 2011 Grade 11/12 Summatives
  20. Sept 2011 Grade 11 Summatives
  21. Sept 2011 Grade 10 Summatives
  22. Feb 2012 Grade 10 Summatives
  23. Sept 2012 Summatives
  24. Feb 2013 Grade 12 Summatives
  25. Feb 2013 2OI Summatives
  26. Sept 2013 Summatives

Strings

Strings are a character array in disguise. Learning this will allow you to grab individual letters from words with ease.

Lots of contest problems are rooted in your ability to assess individual characters in sentences. Also in this Skill Set, you will be introduced to file i/o: input and output. This will allow you to get information from an external text file. All programming contests are rooted in reading standard information from a text file, so it will be included in this skill set.

We can go back to Skill Set 2: user input with an External File. Back then, we asked the user for their name, and then we outputted a greeting.

We populated the sName variable from an Input TextField by way of:

sName = txtName.text;   
That was within the EventListener.

The String Class has many functions that are available to you. For example, once sName is populated, you can:

trace (sName.length); // this will output the size of the String.

The following code displays each letter in sName as well as the ASCII value:

for (var i:int = 0; i < sName.length; i++)
{
   trace(sName.charAt(i), "-", sName.charCodeAt(i));
}

You can turn numbers into Strings:

var nNum:Number = 23.57;  
var str:sNum = nNum.toString();  

You can "concatenate" Strings (combine them):

sFullName = sFirst + sLast;

You can "Slice" up a String:

trace(sName.substr(0,2));
trace (sName.slice(-2,sName.length));

The first example gets a "subString" of the String, and gets the chars starting with the first letter (index 0), and up to, but not including the char at index 2.

The slice function grabs the char second from the end up to but not including the index of the lenght. Since Strings start at index 0, the index of the length of the array is one bigger than the array itself.

Finding chars within a String:

sSentence = "The cake is a lie"; 
trace(sSentence.indexOf("is")); // output: 9

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:

trace(sSentence.indexOf("e",2)); // output: 2
trace(sSentence.indexOf("e",3)); // output: 7

In the first example, since there is an "e" at index 2, 2 is outputted. In the second example, the next occurence of "e" after index 3 is 7.

The value of -1 will be returned if no occurence can be found.

The lastIndexOf() method finds the last occurrence of a substring in the string.

Other String Methods

 

Method

Syntax

Description

charAt

myString.charAt(pos)

Returns the character at the position (pos).

charCodeAt

String.charCodeAt(pos)

Returns the character code of the character at the position (pos).

fromCharCode

String.fromCharCode(num)

Returns the character from the character code number (num).

indexOf

myString.indexOf
(innerString,startPos)

Returns the location of the inner string in the main string.

replace

myString.replace(regexp,replacement)

Replaces the pattern within the string.

slice

myString.slice(start,end)

Returns the substring starting at position (start) and ending at position (end).

String

String(notAString)

Converts a number or other value to a string.

substr

myString.substr(start,len)

Returns the substring starting at position (start) of length (len).

toLowerCase

myString.toLowerCase()

Returns the string with lower case letters.

toUpperCase

myString.toUpperCase()

Returns the string with upper case letters.

concat

myString.concat(otherString)

Returns a new string with the second string appended to end of the first string.

length

myString.length

Returns the number of characters in the string.

 

There's more at adobe's site.