Home Tutorial Flex Flex4 Components String substr method example

 
 

String substr method example
Posted on: June 4, 2010 at 12:00 AM
This method takes two integer parameters, first is starting character position and second is length of the substring.

String substr() method example:-

String class provides substr() method for find substring from string. This method takes two integer parameters, first is starting character position and second is length of the substring.

Example:-

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init();">

<fx:Script>

<![CDATA[

private function init():void{

var str:String = "Rose India New Delhi";

var substring:String = str.substr(8,3);

textcontrol1.text = str;

textcontrol2.text = substring;

}

]]>

</fx:Script>

<s:Panel width="250" height="200" title="String substr() method example" backgroundColor="0xEEFFEE">

<s:VGroup>

<mx:Text id="textmsg1" text=" Main String" fontSize="12" fontWeight="bold"/>

<mx:Text id="textcontrol1"/>

<mx:Text id="textmsg2" text="Sub String" fontSize="12" fontWeight="bold"/>

<mx:Text id="textcontrol2"/>

</s:VGroup>

</s:Panel>

</s:Application>

In this example, we have used this method with a string as "Rose India New Delhi" and find string that starting character position is 8 and length of the substring is 3.

Output:-

Download this code

Related Tags for String substr method example:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.