String slice method example


 

String slice method example

The string slice() and substring() methods functionality is same but difference is that slice() can take negative integer parameters.

The string slice() and substring() methods functionality is same but difference is that slice() can take negative integer parameters.

String slice() method example:-

The string slice() and substring() methods functionality is same but difference is that slice() can take negative integer parameters. If passing parameters value is negative that means slice() find substring end of the string. In this example user can see how to use this method.

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";

// first type

var substring1:String = str.slice(-3,-1);

// second type

var substring:String = str.slice(-5,13);

// third type

var substring2:String = str.slice(-3, str.length);

textcontrol1.text = str;

textcontrol2.text = substring1;

textcontrol3.text = substring;

textcontrol4.text = substring2;

}

]]>

</fx:Script>

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

<s:VGroup>

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

<mx:Text id="textcontrol1"/>

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

0

<mx:Text id="textcontrol2"/>

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

<mx:Text id="textcontrol3"/>

1

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

<mx:Text id="textcontrol4"/>

</s:VGroup>

2

</s:Panel>

</s:Application>

In this example we have used slice() method in different types.

3

Output:-

Download this code

4

Ads