/* related posts with thumb nails */

Program to demonstrate String functions:

class StringTest

{

public static void main(String as[])

{

String s1="abc",s2="xyz";

System.out.println("Length :"+s1.length());

System.out.println("Concatenation :"+s1.concat(s2));

System.out.println("UpperCase :"+s1.toUpperCase());

System.out.println("LowerCase :"+s1.toLowerCase());

System.out.println("substring (1,2):"+s1.substring(2,2));

System.out.println("replace :"+s1.replace('c','x'));

System.out.println("Ends with bc:"+s1.endsWith("bc"));

System.out.println("Starts with bc:"+s1.startsWith("bc"));

System.out.println("Char at Pos 1:"+s1.charAt(1));

/* Searching Functions */

System.out.println("\nSearching Functions\n");

String s="This is his wish";

System.out.println("First occurrence of is:"+s.indexOf("is"));

System.out.println("First occurrence of is after 5 chars:"+s.indexOf("is",5));

System.out.println("Last occurrence of is:"+s.lastIndexOf("is"));

System.out.println("Last occurrence of is in 10 chars span:"+s.lastIndexOf("is",10));

/* Comparison Functions */

System.out.println("\n Comparison Functions\n");

String x="RAVI", y="RAMU", z="ravi";

int k=x.compareTo(y);

if(k>0)

System.out.println(x+" is Big");

else if(k<0)

System.out.println(y+" is Big");

else

System.out.println(x+" and "+y+" are Equal");

if(x.equals(y))

System.out.println(x+" and "+y+" are Equal");

else

System.out.println(x+" and "+y+" are Not Equal");

if(x.equalsIgnoreCase(z))

System.out.println(x+" and "+z+" are Equal");

else

System.out.println(x+" and "+z+" are Not Equal");

}

}

/* Output: */

Length :3

Concatenation :abcxyz

UpperCase :ABC

LowerCase :abc

substring (1,2):a

replace :abx

Ends with bc:true

Starts with bc:false

Char at Pos 1:b

Searching Functions

First occurrence of is:2

First occurrence of is after 5 chars:5

Last occurrence of is:13

Last occurrence of is in 10 chars span:9

Comparison Functions

RAVI is Big

RAVI and RAMU are Not Equal

RAVI and ravi are Equal

Related Topics:

0 comments:

Post a Comment