Introduction To String

String :

A String is an array of Unicode Characters. But like Array we can perform all the operations on String .

But there are some differences based on programming language.

Operations&Functions On Strings :

Compare Function :

String has its own compare function.

"==" Can be used in Java to compare whether the two objects are same but cannot be used to compare two Strings.

"==" Can be used in C++ to Compare Two Strings as it supports Operator Overloading.


Example :


package com.techyield.operationsinarray;

public class OperationsInString {

public static void main(String[] args) {
String s1 = "Hello World! Chitti Version 2.0";
// s2 is reference of s1
String s2 = s1;
// s3 is copy of s1
String s3 = new String(s1);
System.out.println("s1 is \""+s1+"\"");
System.out.println("s2 is "+s2);
System.out.println("s3 is "+s3);
// compare using ==
System.out.println("Compare Strings using \"==\"");
System.out.println("Comparing s1 and s2"+ (s1==s2));
System.out.println("Comparing s2 and s3 using \"==\""+ (s2==s3));
System.out.println("Comparing s1 and s3 using \"==\""+ (s1==s3));

// compare using equals method
System.out.println("Compare Strings using equals method");
System.out.println("Comparing s1 and s2"+ (s1.equals(s2)));
System.out.println("Comparing s2 and s3 using \"==\""+ (s2.equals(s3)));
System.out.println("Comparing s1 and s3 using \"==\""+ (s1.equals(s1)));

// compare using compareTo method
System.out.println("Compare Strings using compareTo method");
System.out.println("Comparing s1 and s2"+ (s1.compareTo(s2) == 0));
System.out.println("Comparing s2 and s3 using \"==\""+ (s2.compareTo(s3) == 0));
System.out.println("Comparing s1 and s3 using \"==\""+ (s1.compareTo(s1) == 0));

}


}


No comments:

Post a Comment

Featured Post

H1B Visa Stamping at US Consulate

  H1B Visa Stamping at US Consulate If you are outside of the US, you need to apply for US Visa at a US Consulate or a US Embassy and get H1...