2.07k likes | 4.65k Vues
Strings in JAVA. String is a sequence of characters. Java implements strings as objects of type String. It belongs to java.lang . We can declare a String variable and directly store a String literal using assignment operator. String str = "Hello";
E N D
String is a sequence of characters. • Java implements strings as objects of type String. • It belongs to java.lang.
We can declare a String variable and directly store a String literal using assignment operator. • String str = "Hello"; • ·We can create String object using new operator with some data. • String s1 = new String ("Java"); • ·We can create a String by using character array also and by passing array name to it, as: • char arr[] = { 'p','r','o',’g’,’r’,’a’,’m’}; • String s2 = new String (arr); • We can create a String by passing array name and specifying which characters we need: • String s3 = new String (str, 2, 3); • Here starting from 2nd character a total of 3 characters are copied into String s3.
Various String Functions String Length: int length(); Example: char st [ ] = {‘a’, ‘b’, ‘c’}; String a = new String (st); System.out.println ( a.length());
Finding String Length Finding string length using the length() method: message = "Welcome"; message.length() //returns 7
String Comparison Boolean regionMatches ( intstartIndex, String str2, int str2Index, int numchars) - compare a specific region inside a string with another specific region in another string. - start Index- specifies the beginning index of the invoking object - str2Index- specifies the beginning index of the object str2 - numchars – specifies the number of characters to be compared. StartsWith () and endsWith () Example: “Football”. endsWith ( “ball”); --- returns true “Football”. startsWith (“wood”); ----returns false
String Comparison intcompareTo (String str) - less than 0: if invoking string is less than str -greater than 0: if invoking string is greater than str -0: if equal - case sensitive.
String Comparisons, cont. • compareTo(Object object) String s1 = new String("Welcome“); String s2 = "welcome"; if (s1.compareTo(s2) > 0) { // s1 is greater than s2 } else if (s1.compareTo(s2) == 0) { // s1 and s2 have the same contents } else // s1 is less than s2
String Comparisons • equals String s1 = new String("Welcome“); String s2 = "welcome"; if (s1.equals(s2)){ // s1 and s2 have the same contents } if (s1 == s2) { // s1 and s2 have the same reference }
equals () versus == equals () method compares the characters within a String object. The == operator compares two object references to see whether they refer to the same object. Example: String s1 = “hello”; String s2 = new String(s1); System.out.println(s1 + “ equals “+s2 + “ ”+ s1.equals(s2)); System.out.println(s1 + “ equals “+s2 + “ ”+ (s1==s2)); Output: hello equals hello true hello equals hello false
Retrieving Individual Characters in a String • Do not use message[0] • Use message.charAt(index) • Index starts from 0
String Concatenation String s3 = s1.concat(s2); String s3 = s1 + s2; s1 + s2 + s3 + s4 + s5 same as (((s1.concat(s2)).concat(s3)).concat(s4)).concat(s5);
Extracting Substrings You can extract a single character from a string using the charAt method. You can also extract a substring from a string using the substring method in the String class. String s1 = "Welcome to Java"; String s2 = s1.substring(0, 11) + "HTML";
Examples "Welcome".toLowerCase() returns a new string, welcome. "Welcome".toUpperCase() returns a new string, WELCOME. " Welcome ".trim() returns a new string, Welcome. "Welcome".replace('e', 'A') returns a new string, WAlcomA. "Welcome".replaceFirst("e", "AB") returns a new string, WABlcome. "Welcome".replaceall("e", "AB") returns a new string, WABlcomAB. "Welcome".replace("el", "AB") returns a new string, WABcome.
Program 3: Write a program using some important methods of String class. // program using String class methods class StrOps { public static void main(String args []) { String str1 = "When it comes to Web programming, Java is #1."; String str2 = new String (str1); String str3 = "Java strings are powerful."; int result, idx; char ch; System.out.println ("Length of str1: " + str1.length ()); // display str1, one char at a time. for(inti=0; i < str1.length(); i++) System.out.print (str1.charAt (i)); System.out.println (); if (str1.equals (str2) ) System.out.println ("str1 equals str2"); }
else System.out.println ("str1 does not equal str2"); if (str1.equals (str3) ) System.out.println ("str1 equals str3"); else System.out.println ("str1 does not equal str3"); result = str1.compareTo (str3); if(result == 0) System.out.println ("str1 and str3 are equal"); else if(result < 0) System.out.println ("str1 is less than str3"); else System.out.println ("str1 is greater than str3"); str2 = "One Two Three One"; // assign a new string to str2 idx = str2.indexOf ("One"); System.out.println ("Index of first occurrence of One: " + idx); idx = str2.lastIndexOf("One"); System.out.println ("Index of last occurrence of One: " + idx);
Questions // index 012345678901 String s1 = "Stuart Reges"; String s2 = "Marty Stepp"; System.out.println(s1.length()); // 12 System.out.println(s1.indexOf("e")); // 8 System.out.println(s1.substring(7, 10)); // "Reg" String s3 = s2.substring(1, 7); System.out.println(s3.toLowerCase()); // "arty s" • Given the following string: // index 0123456789012345678901 String book = "Building Java Programs"; • How would you extract the word "Java" ?
Write a program that outputs a person's Name as • first initial • Diddy • last name (all caps) • first name • -izzle Example Output: Type your name, playa: Marge Simpson Your gangsta name is "M. Diddy SIMPSON Marge-izzle"
Problem: Finding Palindromes • Objective: Checking whether a string is a palindrome: a string that reads the same forward and backward. Run CheckPalindrome