2. String class
The String class represents character strings. All string literals in Java
programs, such as "abc", are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are
created.
For example:
String str = "abc"; is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
3. Methods
The class String includes methods for
examining individual characters of the sequence,
for comparing strings,
for searching strings,
for extracting substrings, and
for creating a copy of a string
with all characters translated to uppercase or to lowercase
4. Concatenation
String name = “Saira";
String introduction = "Hello there, my name is " + name;
concat(String str)
Concatenates the specified string to the end of this string.
public class test
{
public static void main(String [] args)
{
String str = "hello";
String str1 = "World";
String str2=str.concat(str1);
System.out.println(str2);
}
}
5. Character at given position
getting the character at a given position within the string --
charAt()
public class test
{
public static void main(String [] args)
{
String str = "hello";
char c= str.charAt(1);
System.out.print(c);
}
}
6. getting the number of characters in a
string -- length()
public class test
{
public static void main(String [] args)
{
String str = "hello";
int x= str.length();
System.out.print(x);
}
}
7. extracting a substring from a string --
substring()
public class test
{
public static void main(String [] args)
{
String str = "hello World";
String str1= str.substring(2,8);
System.out.print(str1);
}
}
8. Compare two strings for equality – equals()
public class test
{
public static void main(String [] args)
{
String str = "hello";
String str1 ="hello";
System.out.println(str.equals(str1));
}
}
9. Compare two strings – compareTo()
=0 indicate equality
>0 indicate calling string follows argument string
<0 indicate calling string precedes argument string
public class test
{
public static void main(String [] args)
{
String str = "hello";
String str1 ="abcde";
System.out.println(str.compareTo(str1));
}
}
10. Tests if this string ends with the specified suffix.
public class test
{
public static void main(String [] args)
{
String str = "hello world";
String str1 ="ld";
System.out.println(str.endsWith(str1));
}
}
11. Replacing all occurrences of oldChar in string with
newChar
public class test
{
public static void main(String [] args)
{
String str = "hello world";
String str1 = str.replace('l','x');
System.out.println(str1);
}
}
12. Converts String to character array
public class test
{
public static void main(String [] args)
{
String str = "hello world";
char []array =str.toCharArray();
System.out.println(array);
System.out.println(array[0]);
}
}
13. Converts all characters to lowercase or uppercase
public class test
{
public static void main(String [] args)
{
String str = "hello world";
String str1 = str.toUpperCase();
String str2= "HHHElloooo";
String str3 = str2.toLowerCase();
System.out.println(str1);
System.out.println(str3);
}
}