Upload
fraz-bakhsh
View
286
Download
3
Tags:
Embed Size (px)
Citation preview
STRINGS
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);
MethodsThe 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 stringwith all characters translated to uppercase or to
lowercase
ConcatenationString 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);
}
}
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);
}}
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);
}}
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);
}}
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));
}}
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));
}}
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));
}}
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);
}}
Converts String to character arraypublic 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]);
}}
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);
}}