Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

How to extract a Substring from a String

This is a discussion on How to extract a Substring from a String within the Tutorials forums, part of the Flash English category; Did it never occur to you, working with text strings, the need to extract only a part of that string? ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 27-09-07, 09:57
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
How to extract a Substring from a String

Did it never occur to you, working with text strings, the need to extract only a part of that string?
In this article, I will show you how to use the substr() and substring() methods of the String Class in Actionscript 3.0.
Both of them returns a string value based on the 2 parameters received.

Let's take a look at them...

The substr() method.

wants two pararmeters:
- startIndex: an integer that indicates the position of the first character to be used
- length: the number of characters extracted after the startIndex

The substring() method.

wants two parameters:
- startIndex: an integer that indicates the position of the first character to be used
- endIndex: an integer that indicates the position of the last character to be used

To better understand these 2 methods, I created a Document Class as example:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Substringa extends MovieClip
	{
		public function Substringa()
		{
			init();
		}
		
		private function init():void
		{
			var testo:String='FlepStudio';
			
			trace(testo.substr(0,4));
			trace(testo.substring(0,4));
			trace('-------------------------');
			
			trace(testo.substr(1,5));
			trace(testo.substring(1,5));
			trace('-------------------------');
			
			trace(testo.substr(4,6));
			trace(testo.substring(4,6));
			trace('-------------------------');
		}
	}
}
The output returned by Flash is the following:
Quote:
Flep
Flep
-------------------------
lepSt
lepS
-------------------------
Studio
St
-------------------------
Let's analyse the code.

I have a String variable with some text
var testo1:String='FlepStudio';

First case:

I use substr() passing the values: zero as startIndex (so it will start from the first character) and 4 as length (it will move of 4 positions starting from the first character)
trace(testo1.substr(0,4));
I use substring() passing the values: zero as startIndex (so it will start from the first character) and 4 as endIndex (it will move till the fourth position and stop)
trace(testo1.substring(0,4));

Second case:

I use substr() passing the values: 1 as startIndex (so it will start from the second character) and 5 as length (it will move of 5 positions starting from the second character)
trace(testo2.substr(1,5));
I use substring() passing the values: 1 as startIndex (so it will start from the second character) and 5 as endIndex (it will move till the fifth position and stop)
trace(testo2.substring(1,5));

Third case:

I use substr() passing the values: 4 as startIndex (so it will start from the fifth character) and 6 as length (it will move of 6 positions starting from the fifth character)
trace(testo.substr(4,6));
I use substring() passing the values: 4 as startIndex (so it will start from the fifth character) and 6 as endIndex (it will move till the sixth position and stop)
trace(testo.substring(4,6));

Stay tuned !
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Actionscript 3 Conversione String -> Class jseeker Actionscript 3.0 base 7 21-08-08 18:11


All times are GMT. The time now is 12:45.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap