14. Strings
Sunday, September 6, 2009
In this chapter you will learn, how to create and manipulate immutable character string objects of a class String and also how to create and manipulate mutable character string objects of class StringBuffer.
Class String
String objects are immutable which means, their character contents cannot be changed after they are created, because class String does not provide any methods that allow the contents of a String object to be modified.
Class String is used to represent strings in Java. This class provides constructors for initializing String objects in a variety of ways.
Have a look at the fallowing example, how to construct String Objects.
When we compile the above program we can see the fallowing output.
st1=
st2=Welcome
st3=javacircuit
st4=avaci
st5=ABCDE
String methods length, charAt
String methods length, charAt returns the length of a string, obtain the character at a specific location in a string respectively.
Here is an example demonstrates the above String methods.
When we compile the above program we can get the fallowing output.
st : Java Circuit
The Length of St is :12
Character at Position 10 is : i
The Reverse of the String St
t i u c r i C a v a J
Comparing Strings
Class String provides several methods for comparing strings, here I am going to demonstrate using the fallowing examples.
Example –I
When we compile this program, we can see the fallowing output.
s1=hello
s2=goodbye
s3=Java Circuit
s4=java circuit
s1 equals "hello"
s1 is not the same object as "hello"
Java Circuit equal java circuit with case ignored
Note I :Comparing references with == can lead to errors, because == compares the references to determine whether they refer to the same object, not whether two object have the same contents.
Note II :When two identical objects are compared with ==, the result will be false.When comparing objects to determine whether they have the same contents, use method equals.
Locating Characters and Sub strings in Strings
Example-II
When we compile this program, we can get the fallowing output.
"started" starts with "st"
"starting" starts with "st"
"started" starts with "art" at position 2
"starting" starts with "art" at position 2
"started" ends with "ed"
"ended" ends with "ed"
Example-III
String Concatenation
Example IV
When we compile this program, we can get the fallowing output.
s1=Welcome
s2=Java Circuit
Result of s1.concat(s2) = WelcomeJava Circuit
s1 after concatenation =Welcome
Example V
When you compile the above program, the output will be as fallows
s1.toUpperCase()=HELLO
s2.toLowerCase()=java circuit
Replace 'l' with 'L' in s1 : heLLo
In the next post we are going to discuss more about Strings, keep an eye on the next post folks !!!
Class String
String objects are immutable which means, their character contents cannot be changed after they are created, because class String does not provide any methods that allow the contents of a String object to be modified.
Class String is used to represent strings in Java. This class provides constructors for initializing String objects in a variety of ways.
Have a look at the fallowing example, how to construct String Objects.
// Constructing String Class /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here char array[]={'j','a','v','a','c','i','r','c','u','i','t'}; char ascii[]={65,66,67,68,69}; String st=new String("Welcome"); // various types of String Constructors String st1=new String(); String st2=new String(st); String st3=new String(array); String st4=new String(array,1,5); String st5=new String(ascii); System.out.printf("st1= %s\nst2=%s\nst3=%s\nst4=%s\nst5=%s\n",st1,st2,st3,st4,st5); } }
When we compile the above program we can see the fallowing output.
st1=
st2=Welcome
st3=javacircuit
st4=avaci
st5=ABCDE
String methods length, charAt
String methods length, charAt returns the length of a string, obtain the character at a specific location in a string respectively.
Here is an example demonstrates the above String methods.
// Demo of length, charAt methods /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here String st="Java Circuit"; System.out.printf("st : %s\n", st); int l=st.length(); System.out.printf("The Length of St is :%d\n", l); int pos=st.charAt(10); // charAt returns ASCII integer so we need conversion System.out.printf("Character at Position 10 is : %c\n", pos); // Reverse a String using lenth and charAt methods System.out.println("The Reverse of the String St"); for(int i=st.length()-1;i>=0;i--) { System.out.printf("%s ", st.charAt(i)); } } }
When we compile the above program we can get the fallowing output.
st : Java Circuit
The Length of St is :12
Character at Position 10 is : i
The Reverse of the String St
t i u c r i C a v a J
Comparing Strings
Class String provides several methods for comparing strings, here I am going to demonstrate using the fallowing examples.
Example –I
// String methods equals, equalsIgnoreCase /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here String s1=new String("hello"); String s2="goodbye"; String s3="Java Circuit"; String s4="java circuit"; System.out.printf("s1=%s\ns2=%s\ns3=%s\ns4=%s\n",s1,s2,s3,s4); // Test for equality with equals method if(s1.equals("hello")) { System.out.println("s1 equals \"hello\""); } else { System.out.println("s1 does not equal \"hello\""); } // Test for equality with == if(s1=="hello") { System.out.println("s1 is the same object as \"hello\""); } else { System.out.println("s1 is not the same object as \"hello\""); } // Test for equality with ignoreCase if(s3.equalsIgnoreCase(s4)) { System.out.printf("%s equal %s with case ignored\n", s3,s4); } else { System.out.println("s3 does not equal s4"); } } }
When we compile this program, we can see the fallowing output.
s1=hello
s2=goodbye
s3=Java Circuit
s4=java circuit
s1 equals "hello"
s1 is not the same object as "hello"
Java Circuit equal java circuit with case ignored
Note I :Comparing references with == can lead to errors, because == compares the references to determine whether they refer to the same object, not whether two object have the same contents.
Note II :When two identical objects are compared with ==, the result will be false.When comparing objects to determine whether they have the same contents, use method equals.
Locating Characters and Sub strings in Strings
Example-II
// startsWith and endsWith demonstration /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here String strings[]={"started","starting","ended","ending"}; // method startsWith for(String string:strings) { if(string.startsWith("st")) { System.out.printf("\"%s\" starts with \"st\"\n",string); } } System.out.println(); // method startsWith along with position for(String string:strings) { if(string.startsWith("art",2)) { System.out.printf("\"%s\" starts with \"art\" at position 2\n", string); } } System.out.println(); // method endsWith for(String string:strings) { if(string.endsWith("ed")) { System.out.printf("\"%s\" ends with \"ed\"\n", string); } } } }
When we compile this program, we can get the fallowing output.
"started" starts with "st"
"starting" starts with "st"
"started" starts with "art" at position 2
"starting" starts with "art" at position 2
"started" ends with "ed"
"ended" ends with "ed"
Example-III
// Demonstration of SubStrings /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here String st="abcdefghijklmnopqrst"; System.out.printf("Substring from index 10 end is : %s\n",st.substring(10)); System.out.printf("Substring from index 3 up to, but not including 6 is %s\n", st.substring(3,6)); } }
String Concatenation
Example IV
// Using concat method /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here String s1="Welcome"; String s2="Java Circuit"; System.out.printf("s1=%s\ns2=%s\n\n",s1,s2); System.out.printf("Result of s1.concat(s2) = %s\n", s1.concat(s2)); System.out.printf("s1 after concatenation =%s\n", s1); } }
When we compile this program, we can get the fallowing output.
s1=Welcome
s2=Java Circuit
Result of s1.concat(s2) = WelcomeJava Circuit
s1 after concatenation =Welcome
Example V
// Using toUpperCase, toLowerCase and replace methods /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here String s1=new String("hello"); String s2=new String("JAVA CIRCUIT"); System.out.printf("s1.toUpperCase()=%s\n",s1.toUpperCase()); System.out.printf("s2.toLowerCase()=%s\n", s2.toLowerCase()); System.out.printf(" Replace 'l' with 'L' in s1 : %s\n",s1.replace('l', 'L')); } }
When you compile the above program, the output will be as fallows
s1.toUpperCase()=HELLO
s2.toLowerCase()=java circuit
Replace 'l' with 'L' in s1 : heLLo
In the next post we are going to discuss more about Strings, keep an eye on the next post folks !!!
Labels:
Strings