What is String Class in Java ?
Simple way to create string in java.
String s="Welcome to my Blog"; //String s contains Welcome to my Blog
Few methods of String Class are as follows:-
If you have More time there is a video you can look and learn from it
For video in Hind language click here
Program:
package xyz;
public class stringexp {
public static void main(String[] args) {
String s1="Welcome to Java";
String s2 = s1;
String s3 = new String("Welcome to Java");
String s4 = "Welcome to Java";
System.out.println("this are four strings"+"\n"+s1+"\n"+s2+"\n"+s3+"\n"+s4);
System.out.println("s1==s2:"+s1==s2);
System.out.println("s2==s3:"+s2==s3);
System.out.println("s1.equals(s2):"+s1.equals(s2));
System.out.println("s2.equals(s3):"+s2.equals(s3));
System.out.println(" s1.compareTo(s2): " +s1.compareTo(s2));
System.out.println("s2.compareTo(s3):" +s2.compareTo(s3));
System.out.println("s1 == s4:"+s1 == s4 );
System.out.println("character at 0:"+s1.charAt(0));
System.out.println("index of string j:"+s1.indexOf('J'));
System.out.println("index of string to:"+s1.indexOf("to"));
System.out.println("last index of a:"+s1.lastIndexOf('a'));
System.out.println("last index of o:"+s1.lastIndexOf("o", 15));
System.out.println("lenth of string:"+s1.length());
System.out.println("from 5th character of string: "+s1.substring(5));
System.out.println("from 5th to 11th character:"+s1.substring(5, 11));
System.out.println("check if it start with wel :"+s1.startsWith("Wel"));
System.out.println("check if it ends with (java):"+s1.endsWith("Java"));
System.out.println("string to lowercase:"+s1.toLowerCase());
System.out.println("string to uppercase:"+s1.toUpperCase());
System.out.println("Welcome "+s1.trim());
System.out.println("replace o with T: "+s1.replace('o', 'T'));
System.out.println("replace all the 'o' by character 'T':"+s1.replaceAll("o", "T"));
System.out.println("replace only 1st 'o' with 'T':"+s1.replaceFirst("o", "T"));
char[] array = s1.toCharArray();
System.out.println(" convert sting to character : ");
for(int i=0;i {
System.out.print(array[i]+" ");
}
}
}
- String class represent character string. All string in java implemented as instance of class.
- String class is an Immutable java class It can not be modified and it uses string table.
- String class in java is one which is used most.
Simple way to create string in java.
String s="Welcome to my Blog"; //String s contains Welcome to my Blog
Few methods of String Class are as follows:-
- Char CharAt(int index) :- Returns a character at specified index.
- int indexOf(char ch):- Returns index of first occurrence of specified character.
- int compareTo(object o):- compare string with another object.
- int length():- Returns length of string.
- String toLowerCase(): Converts all the character of string to lower case.
If you have More time there is a video you can look and learn from it
Program:
package xyz;
public class stringexp {
public static void main(String[] args) {
String s1="Welcome to Java";
String s2 = s1;
String s3 = new String("Welcome to Java");
String s4 = "Welcome to Java";
System.out.println("this are four strings"+"\n"+s1+"\n"+s2+"\n"+s3+"\n"+s4);
System.out.println("s1==s2:"+s1==s2);
System.out.println("s2==s3:"+s2==s3);
System.out.println("s1.equals(s2):"+s1.equals(s2));
System.out.println("s2.equals(s3):"+s2.equals(s3));
System.out.println(" s1.compareTo(s2): " +s1.compareTo(s2));
System.out.println("s2.compareTo(s3):" +s2.compareTo(s3));
System.out.println("s1 == s4:"+s1 == s4 );
System.out.println("character at 0:"+s1.charAt(0));
System.out.println("index of string j:"+s1.indexOf('J'));
System.out.println("index of string to:"+s1.indexOf("to"));
System.out.println("last index of a:"+s1.lastIndexOf('a'));
System.out.println("last index of o:"+s1.lastIndexOf("o", 15));
System.out.println("lenth of string:"+s1.length());
System.out.println("from 5th character of string: "+s1.substring(5));
System.out.println("from 5th to 11th character:"+s1.substring(5, 11));
System.out.println("check if it start with wel :"+s1.startsWith("Wel"));
System.out.println("check if it ends with (java):"+s1.endsWith("Java"));
System.out.println("string to lowercase:"+s1.toLowerCase());
System.out.println("string to uppercase:"+s1.toUpperCase());
System.out.println("Welcome "+s1.trim());
System.out.println("replace o with T: "+s1.replace('o', 'T'));
System.out.println("replace all the 'o' by character 'T':"+s1.replaceAll("o", "T"));
System.out.println("replace only 1st 'o' with 'T':"+s1.replaceFirst("o", "T"));
char[] array = s1.toCharArray();
System.out.println(" convert sting to character : ");
for(int i=0;i
System.out.print(array[i]+" ");
}
}
}
Output:
this are four strings
Welcome to Java
Welcome to Java
Welcome to Java
Welcome to Java
false
false
s1.equals(s2):true
s2.equals(s3):true
s1.compareTo(s2): 0
s2.compareTo(s3):0
false
character at 0:W
index of string j:11
index of string to:8
last index of a:14
last index of o:9
lenth of string:15
from 5th character of string: me to Java
from 5th to 11th character:me to
check if it start with wel :true
check if it ends with (java):true
string to lowercase:welcome to java
string to uppercase:WELCOME TO JAVA
Welcome Welcome to Java
replace o with T: WelcTme tT Java
replace all the 'o' by character 'T':WelcTme tT Java
replace only 1st 'o' with 'T':WelcTme to Java
convert sting to character :
W e l c o m e t o J a v a
No comments:
Post a Comment