Tuesday, November 20, 2012

An example including all STRING METHODS !!!

Meaning of String methods used in this example:

toUpperCase = To make all word capital
toLowerCase = To make all word small
concat = To concatenates both word
trim = To trim the unnecessary space from sentence 
replace = To replace old character to new character
indexOf = Showing position of given character
compareTo = compare with another string
compareToIgnoreCase = compare but also ignore on given condition
equals = compare both string, if it is equal than its true 
equalsIgnoreCase = compare both string and also ignore case of some characters 
______________________________________________

public class Stringexample {

static String firstname = new String ("HARDIK");

static String lastname = new String ("VYAS");


public static void main(String[] args) {
// TODO Auto-generated method stub



int f = firstname.length();
int l = lastname.length();

System.out.println("Showing the length of words: "+f+" "+l);
System.out.println("Calling both from string: "+firstname+ " " + lastname);
System.out.println(" ");

String lcase = firstname.toLowerCase();
String lcase1 = lastname.toLowerCase();

System.out.println("Change it to lower case of spelling: "+lcase+" "+lcase1);

String ucase = lcase.toUpperCase();
String ucase1 = lcase1.toUpperCase();

System.out.println("Change it to upper case of spelling: "+ucase+" "+ucase1);

System.out.println(" ");

String c= firstname.concat(lastname);
System.out.println("Use of concat method: "+c);


String senten = (" GAME  IS  OVER ! ");

String sentence =senten.trim(); 

System.out.println("Trim the unnecessary space from sentence: "+ sentence);
System.out.println(" ");
System.out.println("Using replace method: "+senten.replace(' ', '*'));

System.out.println(" ");
String allname = "john! smith. larry, harry/";

int position = allname.indexOf(',');

System.out.println("Position of any symbol or character that you define in index of method:  "+position); 

String name = "john";
String name2 = "johneeeee";

int compare = name.compareTo(name2);

System.out.println("Comapre method: " +compare);

System.out.println(" ");

        int compar = name2.compareToIgnoreCase(name);  // using below if else condition to give output of this kind of boolean method

if(compar==0){
System.out.println("Both r eqaull");
}else if(compar>0){
System.out.println("name2 is greater than name");
}else{
System.out.println("name is greater than name2");
}

System.out.println(" ");

System.out.println(name2.equals(name)); // this is one of boolean method

System.out.println(name.equalsIgnoreCase(name)); // this boolean method is for ignore same word

}
}
___________________________________________

Outout:


Showing the length of words: 6 4
Calling both from string: HARDIK VYAS

Change it to lower case of spelling: hardik vyas
Change it to upper case of spelling: HARDIK VYAS

Use of concat method: HARDIKVYAS
Trim the unnecessary space from sentence: GAME  IS  OVER !

Using replace method: *GAME**IS**OVER*!*

Position of any symbol or character that you define in index of method:  18
Comapre method: -5

name2 is greater than name

false
true
______________________________________

When you start looking at program than you might be think as confusing that from where to start read it or what ever but when you copy it to your tool than you come to know the real scenario.

When you copy this code and try it your self than and only than you come to know the reason or try to copy the same and do some change it and than write new code so that you can better understand your self confidently. 

I hope that above example will help you a lot,

Thank you for your time..............


No comments:

Post a Comment