java Program to illustrate the use of string class constructors and methods.

in this tutorial we are we are cover the topic of string class constructors and methods and program on it 

what is string class?

string is a sequence of  character in java it means sequence which is only having character.the object of string are immutable which means it is constant and cannot change once it create.

what is constructors?

constructor is a special method in java which is used to initialized object. when an object of class is created that time constructor is called.it can be use to set initial values for object attribute. 

what is method?
  1. method is used to exhibits functionality of an object. 
  2. method is invoke explicitly
  3. method may/may not return value 
  4. in the case of method no default method is provided.
  5. method should not having name as that class 

 //java Program to illustrate the use of string class constructors and methods.

import java.io.*;

 

import java.util.*;

 

class test

{

 

public static void main(String[] args)

 

{

 

String s="GeeksforGeeks";

System.out.println("String length: "+s.length());

 

System.out.println("Character at 3rd

 

position:"+s.charAt(3));

 

System.out.println("Substring:"+s.substring(3));

System.out.println("Substring2:"+s.substring(2,5));

 

String s1="Geeks";

 

String s2="forGeeks";

 

System.out.println("concatenated string:"+s1.concat(s2)); String s4="Learn Share Learn"; System.out.println("Index of

 

share:"+s4.indexOf("Share"));

 

System.out.println("Index of a:"+s4.indexOf('a',3));

 

Boolean out="Geeks".equals("geeks");

System.out.println("checking equality:"+out);

 

out="Geeks".equals("Geeks");

 

System.out.println("checking equality:"+out);

 

int out1=s1.compareTo(s2); System.out.println("If s1=s2:"+out1); String word1="GeekyMe"; System.out.println("changing to lower

 

case:"+word1.toLowerCase());

String word2="GeekyME"; System.out.println("changing to upper

 

case:"+word2.toUpperCase());

 

String word4="Learn Share Learn"; System.out.println("Trim the word:"+word4.trim()); String str1="feeksforfeeks"; System.out.println("original String:"+str1);

 

String str2="feeksforfeeks".replace('f','g'); System.out.println("Replaced f with g:"+str2);

 

}

 

}

 

Output :-

java Program to illustrate the use of string class constructors and methods.


Post a Comment

0 Comments