Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,

Similar presentations


Presentation on theme: "String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,"— Presentation transcript:

1

2 String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters, numerical characters and a large set of characters for a variety of purposes like: ! @ # $ % ^ & * ( ) _ +

3 String Literal Definition A string literal is a set of characters delimited with double quotations like: "Seymour Snodgrass" and "SSN: 123-45-6789"

4 // Java1601.java // This program demonstrates how to declare five String objects. // Note that all five string objects store the same information. public class Java1601 { public static void main (String args[]) { String s1 = "Tango"; System.out.println("s1: " + s1); String s2; s2 = "Tango"; System.out.println("s2: " + s2); String s3 = new String("Tango"); System.out.println("s3: " + s3); String s4 = new String(); s4 = "Tango"; System.out.println("s4: " + s4); char Dance[] = {'T','a','n','g','o'}; String s5 = new String(Dance); System.out.println("s5: " + s5); System.out.println(); }

5 Mathematical Addition 100 + 200 = 300 int x = 100; x += 200;

6 String Concatenation “100” + “200” = “100200” String x = “100”; x += “200”;

7 // Java1602.java // This program shows how to concatenate strings using the // + operator and the method. public class Java1602 { public static void main (String args[]) { String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println("s3: " + s3); String s4 = "Argentine"; s4 = s4.concat(" Tango"); System.out.println("s4: " + s4); System.out.println(); }

8 String Method concat s1 = s2.concat("hiss"); concat concatenates the method argument to the object String. If s2 == "boo" then s1 will become "boohiss"

9 // Java1603.java // This program demonstrates the use of the method. public class Java1603 { public static void main (String args[]) { String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println(s1 + " has " + s1.length() + " characters."); System.out.println(s2 + " has " + s2.length() + " characters."); System.out.println(s3 + " has " + s3.length() + " characters."); System.out.println(); }

10 // Java1604.java // This program demonstrates how to access individual characters of // a String object with the method. public class Java1604 { public static void main (String args[]) { String s1 = "Madam I'm Adam"; String s2 = ""; int N = s1.length() - 1; for (int K = N; K >= 0; K--) s2 += s1.charAt(K); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); }

11 String Method charAt letter = s.charAt(k); Method charAt returns the character stored at the k th index location of the String object s. The first character is at index 0. If s == "Aardvark" and k == 4 then letter becomes v

12 // Java1605.java // This program demonstrates how to access specified characters of // a string with the method, where SI is the // StartIndex and EI is one less than the EndIndex. public class Java1605 { public static void main (String args[]) { String s = "Racecar"; int n = s.length(); for (int k = 1; k <= n; k++) System.out.println(s.substring(0,k)); System.out.println(); for (int k = 0; k <= n-3; k++) System.out.println(s.substring(k,k+3)); System.out.println(); }

13 String Method substring s1 = “Aardvark”; s2 = s1.substring(0,k); Method substring returns a set of consecutive characters from String s1, starting at index 0, and ending at index k-1. s3 = s1.substring(4,8); s2 becomes "Aard" and s3 becomes "vark" s1 01234567 Aardvark

14 // Java1606.java // This program shows the method, which returns the index of the first // occurrence of the string argument, and the method, which returns the // index of the last occurrence of the string argument. public class Java1606 { public static void main (String args[]) { String s1 = "racecar"; String s2 = "racecar in the carport"; String s3 = "car"; int index1 = s1.indexOf(s3); int index2 = s1.lastIndexOf(s3); int index3 = s2.indexOf(s3); int index4 = s2.lastIndexOf(s3); int index5 = s1.indexOf("qwerty"); System.out.println("With \"" + s1 + "\" car starts at " + index1 + " and last shows up at " + index2); System.out.println("With \"" + s2 + "\" car starts at " + index3 + " and last shows up at " + index4); System.out.println("With \"" + s3 + "\" Qwerty shows up at " + index5); System.out.println(); }

15 String Methods indexOf & lastIndexOf indexOf returns the first occurrence of a substring s1.indexOf(“hum”); returns 0 s1.indexOf(“ku”); returns 10 s1.indexOf(“qwerty”); returns -1 lastIndexOf returns the last occurrence of a substring s1.lastIndexOf(“hum”); returns 4 s1.lastIndexOf(“ku”); returns 14 s1.lastIndexOf(“qwerty”); returns -1 Note: The i in i ndexOf is lowercase, but in last I ndexOf it’s capital! s1 0123456789101112131415161718192021 humuhumunukunukuapua’a By the way, its the State Fish of Hawaii.

16 // Java1607.java // This program demonstrates the String // method, which replaces all occurrences of the Old character // with the New character. // This method creates a new String object with the replaced // characters. public class Java1607 { public static void main (String args[]) { String s1 = "racecar"; String s2 = s1.replace('r','l'); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); }

17 String Method replace String s2 = s1.replace('m','b'); Method replace returns a string such that every occurrence of the old character ( 'm' ) is replaced by the new character ( 'b' ). If s1 == "madam" then s becomes "badab"

18 // Java1608.java // This program demonstrates the String and // methods. public class Java1608 { public static void main (String args[]) { String s1 = "racecar"; String s2 = "RaCeCaR"; String s3 = "RACECAR100"; String s4 = s1.toUpperCase(); String s5 = s2.toUpperCase(); String s6 = s3.toUpperCase(); System.out.println("s1 --> s4: " + s4); System.out.println("s2 --> s5: " + s5); System.out.println("s3 --> s6: " + s6); System.out.println(); s1 = s4.toLowerCase(); s2 = s5.toLowerCase(); s3 = s6.toLowerCase(); System.out.println("s4 --> s1: " + s1); System.out.println("s5 --> s2: " + s2); System.out.println("s6 --> s3: " + s3); System.out.println(); }

19 String Methods toUpperCase and toLowerCase s1 = s2.toLowerCase; s3 = s1.toUpperCase; Method toLowerCase returns a string with lower-case letters. Method toUpperCase returns a string with upper-case letters. Any characters that are not letters will be ignored by both methods and returned in their same relative string position.

20 // Java1609.java // This program demonstrates the method of the String class, // which is shown to convert four data types to a string. // Note that is a static method and must be called using //. public class Java1609 { public static void main (String args[]) { String s1 = String.valueOf(1000); String s2 = String.valueOf(123.321); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s3: " + s3); System.out.println("s4: " + s4); System.out.println(); }

21 String static Method valueOf String s1 = String.valueOf(1000); String s2 = String.valueOf(123.321); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); Method valueOf converts the provided parameter and returns a string. Four overloaded valueOf methods are displayed. Note that the valueOf method is a static method (or class method) that is called with the String class identifier.

22 // Java1610.java // This program converts string values to integer and double values // using the and methods of the // and classes. public class Java1610 { public static void main (String args[]) { String s1 = "12345"; String s2 = "123.321"; int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); System.out.println(n1 + " + " + n1 + " = " + (n1 + n1)); System.out.println(n2 + " + " + n2 + " = " + (n2 + n2)); System.out.println(); }

23 Integer static method parseInt and Double static method parseDouble int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); Method parseInt converts a String into an int. Method parseDouble converts a String into a double. Parameters that include non-numerical characters will compile, but will cause a run-time error.

24 // Java1611.java // This program checks the equality of two strings with the == operator. // The program executes as you might expect. public class Java1611 { public static void main (String args[]) { String s1 = "Foxtrot"; String s2 = "Waltz"; String s3 = "Foxtrot"; if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

25 // Java1612.java // This program checks equality of strings, but this time a string entered at the keyboard // is used for comparison. This program has unexpected results. import java.util.Scanner; public class Java1612 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.nextLine(); if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

26 // Java1613.java // This program uses the method, which removes "white space" from both ends of // the string argument. This method is used to try and solve the problem of the previous // program. import java.util.Scanner; public class Java1613 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.nextLine(); if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3. trim ()) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

27 String Method trim s1 = s2.trim(); String method trim returns a string with "white space" removed from both ends of the String object.

28 // Java1614.java // This program demonstrates the method, which is capable of // testing equality of string objects correctly. import java.util.Scanner; public class Java1614 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.readLine(); if ( s1.equals(s2) ) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if ( s1.equals(s3) ) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); }

29 What Is Going On? Part 1 s1 @dff6ccd s2 @3b0eb0 dff6ccd Foxtrot 3b0eb0 Waltz s3 @18d107f 18d107f Foxtrot

30 What Is Going On? Part 2 s1 @dff6ccd s2 @3b0eb0 dff6ccd Foxtrot 3b0eb0 Waltz s3 @18d107f 18d107f Foxtrot

31 // Java1615.java // This program demonstrates the method, which returns an integer value. // The value is 0 when the strings are equal, otherwise a value is returned that // indicates the relative distance between the strings. public class Java1615 { public static void main (String args[]) { String s1 = "AARDVARK"; String s2 = "ZEBRA"; String s3 = "AARDVARK"; String s4 = "BART"; int value1 = s1.compareTo(s2); int value2 = s1.compareTo(s3); int value3 = s2.compareTo(s1); int value4 = s1.compareTo(s4); System.out.println("value1: " + value1); System.out.println("value2: " + value2); System.out.println("value3: " + value3); System.out.println("value4: " + value4); System.out.println(); }

32 String methods equals and compareTo if (s1.equals(s2)) int distance = s3.compareTo(s4); Method equals returns true is s1 == s2, and false otherwise. Method compareTo returns 0 if s3 == s4, otherwise an integer is returned based on the difference between s3 and s4. If s3 < s4, the returned value is negative. If s3 > s4, the returned value is positive.

33 String Objects are Immutable A mutator is a method that mutates or alters object values. The character contents of a String object cannot be altered, which means that String objects are immutable. You need to construct a StringBuffer object if it is necessary to change the character content after the object is constructed.

34 What do you mean "A String cannot be altered?!" If you look at these statements: it sure looks like name is being altered. In reality what happens is the old String object is destroyed and a new object (with a new memory address) is created. String name = "Bob"; name = "Joe";

35 // Java1616.java // This program introduces the class. // Three different approaches are shown to construct a StringBuffer object. // You cannot assign a String to a StringBuffer object. class Java1616 { public static void main (String args[]) { // constructs a StringBuffer object with capacity 16 and no characters StringBuffer s1 = new StringBuffer(); // s1 = "This will not compile"; System.out.println("s1: " + s1); // constructs a StringBuffer object with capacity 50 and no characters StringBuffer s2 = new StringBuffer(50); System.out.println("s2: " + s2); // constructs a StringBuffer object with required capacity and stores the string argument StringBuffer s3 = new StringBuffer("The quick brown fox jumps over the lazy dog"); System.out.println("s3: " + s3); System.out.println(); }

36 // Java1617.java // This program demonstrates the method, which inserts a new string at a // specified location. // The method automatically allocates the required StringBuffer space. public class Java1617 { public static void main (String args[]) { StringBuffer s1 = new StringBuffer(); s1.insert(0,"The quick brown fox jumps over the lazy dog"); System.out.println("s1: " + s1); StringBuffer s2 = new StringBuffer(50); s2.insert(0,"The quick brown fox jumps over the lazy dog"); System.out.println("s2: " + s2); StringBuffer s3 = new StringBuffer("The quick brown fox jumps over the lazy dog"); s3.insert(4,"ever so "); System.out.println("s3: " + s3); System.out.println(); }

37 StringBuffer Method insert s.insert(index,newString); Method insert adds newString to the existing StringBuffer object s, at the index character location.

38 // Java1618.java This program demonstrates the, and methods. // length returns the size of the stored String. capacity returns the size of the StringBuffer, // and setLength alters the size of the StringBuffer. // Note how 16 extra spaces are constructed with a string argument construction. public class Java1618 { public static void main (String args[]) { StringBuffer s1 = new StringBuffer(); System.out.println("Length: " + s1.length()); System.out.println("Capacity: " + s1.capacity()); s1.insert(0,"The quick brown fox jumps over the lazy dog"); System.out.println("Length: " + s1.length()); System.out.println("Capacity: " + s1.capacity()); System.out.println(); StringBuffer s2 = new StringBuffer(50); System.out.println("Length: " + s2.length()); System.out.println("Capacity: " + s2.capacity()); s2.insert(0,"The quick brown fox jumps over the lazy dog"); System.out.println("Length: " + s2.length()); System.out.println("Capacity: " + s2.capacity()); System.out.println(); StringBuffer s3 = new StringBuffer("The quick brown fox jumps over the lazy dog"); System.out.println("Length: " + s3.length()); System.out.println("Capacity: " + s3.capacity()); s3.insert(4,"ever so "); System.out.println("Length: " + s3.length()); System.out.println("Capacity: " + s3.capacity()); System.out.println(); s1.setLength(1000); System.out.println("Length: " + s1.length()); System.out.println("Capacity: " + s1.capacity()); System.out.println(s1); }

39 StringBuffer Methods length, capacity and setLength int len = s.length(); int cap = s.capacity(); s.setLength(1000); Method length returns the size of the StringBuffer object. Method capacity returns buffer capacity for total characters. Method setLength sets the size of the StringBuffer object and appends the string with spaces.

40 // Java1619.java // This program demonstrates how to use the method to // access individual characters in a StringBuffer object. public class Java1619 { public static void main (String args[]) { StringBuffer s1 = new StringBuffer( "Madam I'm Adam"); StringBuffer s2 = new StringBuffer(100); int n = s1.length(); for (int k = 0; k < n; k++) s2.insert(k, s1.charAt(n-k-1)); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); }

41 // Java1620.java // This program demonstrates how the method // replaces characters in a StringBuffer object. public class Java1620 { public static void main (String args[]) { StringBuffer s = new StringBuffer( "Aardvark"); System.out.println(s); s.setCharAt(0,'E'); s.setCharAt(1,'e'); s.setCharAt(5,'e'); System.out.println(s); System.out.println(); }

42 StringBuffer Method setCharAt s.setCharAt(5,'A'); Method setCharAt replaces the character at index 5 of the s object with character 'A'.

43 // Java1621.java // This program demonstrates the method, // which deletes a substring from a StringBuffer object // from StartingIndex to EndIndex-1. public class Java1621 { public static void main (String args[]) { StringBuffer s = new StringBuffer( "The quick brown fox jumps over the lazy dog"); System.out.println(s); s.delete(9,15); System.out.println(s); s.delete(3,9); System.out.println(s); System.out.println(); }

44 StringBuffer Method delete s.delete(4,7); Method delete removes a substring from s starting at index 4 and ending at index 6. If s == "Discombobulated" then after the s.delete(4,7); call, s becomes "Discobulated".

45 // Java1622.java // This program demonstrates the method, // which replaces a substring from a StringBuffer object // from StartingIndex to EndIndex-1 with string S. public class Java1622 { public static void main (String args[]) { StringBuffer s = new StringBuffer( "The quick brown fox jumps over the lazy dog"); System.out.println(s); s.replace(10,15,"red"); System.out.println(s); s.replace(4,9,"slow"); System.out.println(s); System.out.println(); }

46 StringBuffer Method replace s.replace(0,3,"lear"); Method replace deletes a substring of s from start index 0 to last index 2, and replaces this space with "lear". If s == "burning" then after replace, s == "learning".


Download ppt "String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,"

Similar presentations


Ads by Google