Share on Google+Share on Google+

String substr method example

Advertisement
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

Advertisements

Advertisement

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.