Share on Google+Share on Google+

String slice method example

Advertisement
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"/>

<mx:Text id="textcontrol2"/>

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

<mx:Text id="textcontrol3"/>

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

<mx:Text id="textcontrol4"/>

</s:VGroup>

</s:Panel>

</s:Application>

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

Output:-

Download this code

Advertisements

Posted on: June 4, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.