java strings
TRANSCRIPT
JavaStrings
StringDeclaration of StringString s1;
Declare and Initialize a String String s2 = “SIKANDER”;
Length of StringString name = “SIKANDER”;int len = name.length( );
Output : 8
Accessing Individual Elements of StringcharAt(int index);
String Assignment
String Concatenation+
Compare StringString s1 = "ABCD”;String s2 = "ABCD”;if(s1 == s2)
System.out.println("Equal");else
System.out.println("NOT Equal");
In Java, when the“==” operator is used to compare2 objects, it checks to see if the objects refer to the same place in memory.
String s1 = new String("ABCD”);String s2 = new String("ABCD”); if(s1 == s2)
System.out.println("Equal");else
System.out.println("NOT Equal");
To check if strings have same data, use equals
String s1 = new String("ABCD");String s2 = new String("ABCD"); if(s1.equals(s2)) System.out.println("Equal");else System.out.println("NOT Equal");
String Concatination
System.out.println(3 + 5); System.out.println(3 + 5 +
"abcd"); System.out.println("abcd" + 3 +
5); System.out.println("abcd" + 3 *
5);
What is the output of these statements?
String str = "SIKANDER”; System.out.println(str); System.out.println(str.toLowerCase());
String str = "SIKANDER”;
System.out.println(str);str.toLowerCase();System.out.println(str);
String str = "SIKANDER”;System.out.println(str.hashCode());str = str.toLowerCase();System.out.println(str.hashCode());
https://www.hackerrank.com/challenges/camelcase
https://www.hackerrank.com/challenges/pangrams
Count Number of Unique Charactershttps://www.hackerrank.com/challenges/string-construction