String in Java (Methods with examples)
public class StringMethods {
public static void main(String[] args)
{
//string compare
String s="ravi"; // equal method return true or false
String s1="ravi";
String s2="kumar";
String s3="Ravi";
System.out.println(s.equals(s3));
System.out.println(s.equalsIgnoreCase(s3));
System.out.println(s.equals(s2));
System.out.println("== operator compares references not values");
String s4="ravi"; //return true false
String s5="ravi";
String s6="kumar";
String s7="Ravi";
System.out.println(s4==s5);
System.out.println(s4==s7);
System.out.println(s4==s6);
System.out.println("compareTo method return integer value");
String s8="ravi";
String s9="ravi";
String s10="sagar";
String s11="om";
System.out.println(s8.compareTo(s9)); //0 because s8==s9
System.out.println(s8.compareTo(s10)); //-1 because s8(4)<s10(5)
System.out.println(s9.compareTo(s10));
}
}
class StringMethod{
public static void main(String[] args) {
String s="ravi.com";
//System.out.println(s.substring(0,8));
//System.out.println(s.substring(3));
System.out.println(s.trim());
System.out.println(s.charAt(6));
int a=10;
String s1=String.valueOf(a); // convert String like int char float double etc
System.out.println(s1+10);
System.out.println(s.replace("r","p"));
}
}
_
__________________________________________________
class StringBufferExample{
public static void main(String[] args) {
StringBuffer s=new StringBuffer("ravi");
//String s1="ravi";
// s.append("kumar");
//System.out.println(s);
//s.insert(2,"kumar");
//System.out.println(s);
//s.replace(1,2,"f");
//System.out.println(s);
System.out.println(s.reverse());
}
}
String Buffer Example with constructor
class StringPractices {
int roll_no;
String name;
String address;
StringPractices(int roll_no,String name,String address){
this.roll_no=roll_no;
this.name=name;
this.address=address;
}
@Override
public String toString() {
return roll_no+" "+name+" "+address;
}
public static void main(String[] args) {
StringPractices str=new StringPractices(100,"ravi","Delhi");
System.out.println(str);
}
}
___________________________________________________
Methods in Strings.
public class StringPractice {
public static void main(String[] args)
{
String s="Ravi";
s=s.concat("kumar");
System.out.println(s);
String string="ravi kumar sagar";//string literal
System.out.println(string.substring(8));
System.out.println(string.toLowerCase());
System.out.println(string.toUpperCase());
System.out.println(string.trim()); //
System.out.println(string.valueOf(string+10));
System.out.println(string.indexOf("k"));
System.out.println(string.startsWith("PORT",9));
System.out.println(string.startsWith("r"));
String str=new String("PORT : 8080");
System.out.println(str);
char ch[] ={'T','R','U','E'};
String str1=new String(ch);
System.out.println(str1);
String ss=new String("Ravi");
s=ss.concat("kumar");
System.out.println(ss);
String s1=40+10+50+"ravi"+10+20;
System.out.println(s1);
String s2=" ravi kumar sagar";
int s4=s2.lastIndexOf("m",5);
int s5=s2.indexOf(" ");
System.out.println(s4);
//String s4=s2.concat(s3);
System.out.println(s4);
System.out.println(s2.substring(0,5));
System.out.println(s2.toUpperCase());
System.out.println(s2.toLowerCase());
System.out.println(s2.trim());
System.out.println(s2.charAt(5));
System.out.println(s2.length());
System.out.println(s2.replace("ravi","sonu"));
}
}
public class StringMethods {
public static void main(String[] args)
{
//string compare
String s="ravi"; // equal method return true or false
String s1="ravi";
String s2="kumar";
String s3="Ravi";
System.out.println(s.equals(s3));
System.out.println(s.equalsIgnoreCase(s3));
System.out.println(s.equals(s2));
System.out.println("== operator compares references not values");
String s4="ravi"; //return true false
String s5="ravi";
String s6="kumar";
String s7="Ravi";
System.out.println(s4==s5);
System.out.println(s4==s7);
System.out.println(s4==s6);
System.out.println("compareTo method return integer value");
String s8="ravi";
String s9="ravi";
String s10="sagar";
String s11="om";
System.out.println(s8.compareTo(s9)); //0 because s8==s9
System.out.println(s8.compareTo(s10)); //-1 because s8(4)<s10(5)
System.out.println(s9.compareTo(s10));
}
}
class StringMethod{
public static void main(String[] args) {
String s="ravi.com";
//System.out.println(s.substring(0,8));
//System.out.println(s.substring(3));
System.out.println(s.trim());
System.out.println(s.charAt(6));
int a=10;
String s1=String.valueOf(a); // convert String like int char float double etc
System.out.println(s1+10);
System.out.println(s.replace("r","p"));
}
}
_
__________________________________________________
class StringBufferExample{
public static void main(String[] args) {
StringBuffer s=new StringBuffer("ravi");
//String s1="ravi";
// s.append("kumar");
//System.out.println(s);
//s.insert(2,"kumar");
//System.out.println(s);
//s.replace(1,2,"f");
//System.out.println(s);
System.out.println(s.reverse());
}
}
String Buffer Example with constructor
class StringPractices {
int roll_no;
String name;
String address;
StringPractices(int roll_no,String name,String address){
this.roll_no=roll_no;
this.name=name;
this.address=address;
}
@Override
public String toString() {
return roll_no+" "+name+" "+address;
}
public static void main(String[] args) {
StringPractices str=new StringPractices(100,"ravi","Delhi");
System.out.println(str);
}
}
___________________________________________________
Methods in Strings.
public class StringPractice {
public static void main(String[] args)
{
String s="Ravi";
s=s.concat("kumar");
System.out.println(s);
String string="ravi kumar sagar";//string literal
System.out.println(string.substring(8));
System.out.println(string.toLowerCase());
System.out.println(string.toUpperCase());
System.out.println(string.trim()); //
System.out.println(string.valueOf(string+10));
System.out.println(string.indexOf("k"));
System.out.println(string.startsWith("PORT",9));
System.out.println(string.startsWith("r"));
String str=new String("PORT : 8080");
System.out.println(str);
char ch[] ={'T','R','U','E'};
String str1=new String(ch);
System.out.println(str1);
String ss=new String("Ravi");
s=ss.concat("kumar");
System.out.println(ss);
String s1=40+10+50+"ravi"+10+20;
System.out.println(s1);
String s2=" ravi kumar sagar";
int s4=s2.lastIndexOf("m",5);
int s5=s2.indexOf(" ");
System.out.println(s4);
//String s4=s2.concat(s3);
System.out.println(s4);
System.out.println(s2.substring(0,5));
System.out.println(s2.toUpperCase());
System.out.println(s2.toLowerCase());
System.out.println(s2.trim());
System.out.println(s2.charAt(5));
System.out.println(s2.length());
System.out.println(s2.replace("ravi","sonu"));
}
}
Sorting and Searching Programs
Start with your Choice
-Important
Programs for Freshers
- Palindrome Number
- Prime
Number
- Swapping
Number
- Leap Year
- Odd and
Even Number
- Fibonacci
Series
- Armstrong
Number
- Factorial
Number
- Print
Table
Learn Pattern Programs
- Alphabet
pattern program in java
- Star
Pattern Programs in Java
- Square
pattern programs in java
- Diamond
Star pattern programs in java
- Number
Pattern Programs in java
- Diagonal
pattern program in java
Click to learn Array Programs
- 4 ways to
print Array in Java
- 2-D Array
in Java
- Anonymous
Array in Java
- Single
Dimensional Array in Java
- Find
Second Largest Number in Array
- Delete an
Element in Array in Java
- Common
Element in Array in Java
- Missing
Element in Array
- Insert
Element in Array
- Reverse
Array in Java
- Merge
Two Array in Java
- Smallest
and Largest Elements in Array
- Find Odd
and Even Number in Array
Click to Learn MySQL
Learn with us | HTML | CSS | JS | Bootstrap | JAVA | ADV JAVA |
MySQL | GIT
No comments:
Post a Comment