string definition a string is a set of characters that behaves as a single unit. the characters in a...

46

Upload: christine-watts

Post on 13-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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,
Page 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,

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:

! @ # $ % ^ & * ( ) _ +

Page 3: 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,

String Literal Definition

A string literal is a set of characters delimited with double quotations like:

"Seymour Snodgrass" and "SSN: 123-45-6789"

Page 4: 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,

// 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(); }}

Page 5: 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,

Mathematical Addition

100 + 200 = 300

int x = 100;x += 200;

Page 6: 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,

String Concatenation

“100” + “200” = “100200”

String x = “100”;x += “200”;

Page 7: 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,

// Java1602.java// This program shows how to concatenate strings using the // + operator and the <concat> 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(); }}

Page 8: 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,

String Method concat

s1 = s2.concat("hiss");

concat concatenates the method argument to the object String.

If s2 == "boo" then s1 will become "boohiss"

Page 9: 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,

// Java1603.java

// This program demonstrates the use of the <length> 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();

}

}

Page 10: 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,

// Java1604.java// This program demonstrates how to access individual characters of // a String object with the <charAt> 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(); }}

Page 11: 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,

String Method charAt

letter = s.charAt(k);

Method charAt returns the character stored at the kth index location of the String object s.

The first character is at index 0.

If s == "Aardvark" and k == 4 then letter becomes v

Page 12: 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,

// Java1605.java// This program demonstrates how to access specified characters of// a string with the <substring(SI,EI)> 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(); }}

Page 13: 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,

String Method substrings1 = “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"

s10 1 2 3 4 5 6 7

A a r d v a r k

Page 14: 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,

// Java1606.java// This program shows the <indexOf> method, which returns the index of the first// occurrence of the string argument, and the <lastIndexOf> 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(); }}

Page 15: 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,

String MethodsindexOf & lastIndexOf

indexOf returns the first occurrence of a substrings1.indexOf(“hum”); returns 0s1.indexOf(“ku”); returns 10s1.indexOf(“qwerty”); returns -1

lastIndexOf returns the last occurrence of a substrings1.lastIndexOf(“hum”); returns 4s1.lastIndexOf(“ku”); returns 14s1.lastIndexOf(“qwerty”); returns -1

Note: The i in indexOf is lowercase, but in lastIndexOf it’s capital!

s10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

h u m u h u m u n u k u n u k u a p u a ’ a

By the way,its the State Fish of Hawaii.

Page 16: 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,

// Java1607.java// This program demonstrates the <replace(Old,New)> 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(); }}

Page 17: 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,

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"

Page 18: 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,

// Java1608.java// This program demonstrates the String <toUpperCase> and // <toLowerCase> 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(); }}

Page 19: 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,

String Methods toUpperCaseand 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.

Page 20: 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,

// Java1609.java// This program demonstrates the <valueOf> method of the String class,// which is shown to convert four data types to a string. // Note that <valueOf> is a static method and must be called using // <String.valueOf>.

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(); }}

Page 21: 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,

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.

Page 22: 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,

// Java1610.java// This program converts string values to integer and double values // using the <parseInt> and <parseDouble> methods of the // <Integer> and <Double> 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(); }}

Page 23: 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,

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.

Page 24: 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,

// 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(); }}

Page 25: 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,

// 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(); }}

Page 26: 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,

// Java1613.java// This program uses the <trim> 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(); }}

Page 27: 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,

String Method trim

s1 = s2.trim();

String method trim returns a string with "white space" removed from both ends of the String object.

Page 28: 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,

// Java1614.java// This program demonstrates the <equals> 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(); }}

Page 29: 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,

What Is Going On? Part 1

s1

@dff6ccd

s2

@3b0eb0

dff6ccd

Foxtrot

3b0eb0

Waltz

s3

@18d107f

18d107f

Foxtrot

Page 30: 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,

What Is Going On? Part 2

s1

@dff6ccd

s2

@3b0eb0

dff6ccd

Foxtrot

3b0eb0

Waltz

s3

@18d107f

18d107f

Foxtrot

Page 31: 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,

// Java1615.java// This program demonstrates the <compareTo> 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(); }}

Page 32: 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,

String methodsequals 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.

Page 33: 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,

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.

Page 34: 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,

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";

Page 35: 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,

// Java1616.java// This program introduces the <StringBuffer> 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(); }}

Page 36: 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,

// Java1617.java// This program demonstrates the <insert> method, which inserts a new string at a// specified location. // The <insert> 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(); }}

Page 37: 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,

StringBuffer Method insert

s.insert(index,newString);

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

Page 38: 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,

// Java1618.java This program demonstrates the <length>, <capacity> and <setLength> 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);

}}

Page 39: 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,

StringBuffer Methodslength, capacity and setLengthint 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.

Page 40: 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,

// Java1619.java// This program demonstrates how to use the <charAt> 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(); }}

Page 41: 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,

// Java1620.java// This program demonstrates how the <setCharAt> 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(); }}

Page 42: 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,

StringBuffer Method setCharAt

s.setCharAt(5,'A');

Method setCharAt replaces the character at index 5 of the s object with character 'A'.

Page 43: 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,

// Java1621.java// This program demonstrates the <delete(SI,EI)> 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(); }}

Page 44: 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,

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".

Page 45: 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,

// Java1622.java// This program demonstrates the <replace(SI,EI,S)> 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(); }}

Page 46: 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,

StringBuffer Method replaces.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".