Finding substrings and patterns in strings. Adobe Flex, FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0
જાહેરાત
જાહેરાત
You can also use character codes to define a string using the fromCharCode() method, as the following example shows: var myStr:String =
String.fromCharCode(104,101,108,108,111,32,119,111,114,108,100,33);
// Sets myStr to "hello world!"
Comparing strings
You can use the following operators to compare strings: < , <= , != , == , => , and >. These operators can be used with conditional statements, such as if and while , as the following example shows: var str1:String = "Apple"; var str2:String = "apple"; if (str1 < str2)
{ trace("A < a, B < b, C < c, ...");
}
When using these operators with strings, ActionScript considers the character code value of each character in the string, comparing characters from left to right, as in the following: trace("A" < "B"); // true trace("A" < "a"); // true trace("Ab" < "az"); // true trace("abc" < "abza"); // true
Use the == and != operators to compare strings with each other and to compare strings with other types of objects, as the following example shows: var str1:String = "1"; var str1b:String = "1"; var str2:String = "2"; trace(str1==str1b); // true trace(str1==str2); // false var total:uint = 1; trace(str1 == total); // true
Use the === and !== operators to verify that both comparison objects are of the same type, with the same content: var str1:String = "4"; var str2:String = "4"; var total:uint = 4; trace(str1 === str2); // true trace(str1 === total); // false
212 Working with Strings
Obtaining string representations of other objects
You can obtain a String representation for any kind of object. All objects have a toString() method for this purpose: var n:Number = 99.47; var str:String = n.toString();
// str == "99.47"
When using the + concatenation operator with a combination of String objects and objects that are not strings, you do not need to use the toString() method. For details on concatenation, see the next section.
The String() global function returns the same value for a given object as the value returned by the object calling the toString() method.
Concatenating strings
Concatenation of strings means taking two strings and joining them sequentially into one.
For example, you can use the + operator to concatenate two strings: var str1:String = "green"; var str2:String = "ish"; var str3:String = str1 + str2;
// str3 == "greenish"
You can also use the += operator to the produce the same result, as the following example shows: var str:String = "green"; str += "ish";
// str == "greenish"
Additionally, the String class includes a concat() method, as follows: var str1:String = "Bonjour"; var str2:String = "from"; var str3:String = "Paris"; var str4:String = concat(str1, " ", str2, " ", str3)
// str4 == "Bonjour from Paris"
If you use the + operator (or the += operator) with a String object and an object that is not a string, ActionScript automatically converts the nonstring object to a String object in order to evaluate the expression, as shown in this example: var str:String = "Area = "; var area:Number = Math.PI * Math.pow(3, 2);
Concatenating strings 213
str = str + area;
// str == "Area = 28.274333882308138"
However, you can use parentheses for grouping to provide context for the + operator, as the following example shows: trace("Total: $" + 4.55 + 1.45);
//"Total: $4.551.45" trace("Total: $" + (4.55 + 1.45));
//"Total: $6"
Finding substrings and patterns in strings
Substrings are sequential characters within a string. For example, the string "abc" has the following substrings: "" , "a" , "ab" , "abc" , "b" , "bc" , "c" . You can use ActionScript methods to locate substrings of a string.
Patterns are defined in ActionScript by strings or by regular expressions. For example, the following regular expression defines a specific pattern—the letters A, B, and C followed by a digit character (the forward slashes are regular expression delimiters):
/ABC\d/
ActionScript includes methods for finding patterns in strings and for replacing found matches with replacement substrings. These methods are described in the following sections.
Regular expressions can define intricate patterns. For more information, see Chapter 10,
“Using Regular Expressions,” on page 285 .
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
214 Working with Strings
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
Finding substrings and patterns in strings 215
Creating an array of substrings segmented by a delimiter
You can use the split() method to create an array of substrings, which is divided based on a delimiter. For example, you can segment a comma-delimited or tab-delimited string into multiple strings.
The following example shows how to split an array into substrings with the ampersand (&) character as the delimiter: var queryStr:String = "first=joe&last=cheng&title=manager&StartDate=3/6/
65"; var params:Array = queryStr.split("&", 2);
// params == ["first=joe","last=cheng"]
The second parameter of the split() method, which is optional, defines the maximum size of the array that is returned.
You can also use a regular expression as the delimiter character: var str:String = "Give me\t5." var a:Array = str.split(/\s+/);
// a == ["Give","me","5."]
For more information, see Chapter 10, “Using Regular Expressions,” on page 285 and the
ActionScript 3.0 Language Reference .
Finding patterns in strings and replacing substrings
The String class includes the following methods for working with patterns in strings:
■
■
Use the match() and search() methods to locate substrings that match a pattern.
Use the replace() method to find substrings that match a pattern and replace them with a specified substring.
These methods are described in the following sections.
You can use strings or regular expressions to define patterns used in these methods. For more information on regular expressions, see Chapter 10, “Using Regular Expressions,” on page 285 .
216 Working with Strings
ડાઉનલોડ કરો
જાહેરાત