strings chapter 7 csci 1302. csci 1302 – strings2 string comparisons compare string contents with...

22
Strings Chapter 7 CSCI 1302

Upload: june-blankenship

Post on 05-Jan-2016

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

Strings

Chapter 7CSCI 1302

Page 2: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 2

String Comparisons

• Compare string contents with the equals(String s) method not ==

String s0 = “ Java”;String s1 = “Welcome to” + s0;String s2 = Welcome to Java”;

System.out.println(“s1==s2 is “ + (s1==s2)); // false

System.out.println(“s1.equals(s2) is “ + (s1.equals(s2))); // true

Page 3: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 3

String Comparisons

• Can also use the compareTo(String s) method

• Don’t use >, >=, <, or <=s1 = “abc”;s2 = “abg”;s3 = “abc”;s4 = “aba”;s1.compareTo(s2); // returns -4s1.compareTo(s3); // returns 0s1.compareTo(s4); // returns 2

Page 4: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 4

String Comparisons

• Use equalsIgnoreCase(String s) for case-insensitive equality

• Use the regionMatches method for comparing substrings

• Use startsWith(prefix) or endsWith(suffix) to check whether a string starts or ends with a certain substring

Page 5: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 5

String Conversions

• String contents cannot be changed, but new strings can be created and transformed with various methods

“Welcome”.toLowerCase(); // welcome“Welcome”.toUpperCase(); // WELCOME“ Welcome ”.trim(); // Welcome“Welcome”.replace(‘e’,’A’); // WAlcomA“Welcome”.replaceFirst(“e”,”A”); // WAlcome

“Welcome”.replaceAll(“e”,”A”) // WAlcomA

Page 6: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 6

String Conversions

• Use these methods to find the first or last (add last in front of each method name) occurrence of a character or substring in a given string

• All return -1 if it is not foundpublic int indexOf(int ch);public int indexOf(int ch, int from);public int indexOf(String str);public int indexOf(String str, int fromIndex);

Page 7: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 7

String ConversionsExamples

"Welcome to Java".indexOf('W') returns 0"Welcome to Java".indexOf('x') returns -1"Welcome to Java".indexOf('o', 5) returns 9"Welcome to Java".indexOf("come") returns 3"Welcome to Java".indexOf("Java", 5) returns 11

"Welcome to Java".indexOf("java", 5) returns -1

"Welcome to Java".lastIndexOf('a') returns 14

Page 8: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 8

Conversions between Strings and Arrays

• String to char Arraychar[] chars = “Java”.toCharArray();

• Use getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) to replace substringschar[] dst = {‘J’,’A’,’V’,’A’};“SAWS”.getChars(2,3,dst,2);

dst becomes {‘J’,’A’,’W’,’S’}

Page 9: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 9

Conversions between Strings and Arrays

• Convert array of characters into a stringString str = new String(new char[]{‘J’,’a’,’v’,’a’});String str = String.valueOf(new char[]{‘J’,’a’,’v’,’a’});

Page 10: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 10

Conversions between Strings and Arrays

• Convert other types to strings• Use overloaded versions of valueOf to

convert char, double, long, int, and float.

String str = new String.valueOf(5.44);String str = String.valueOf(3);

Page 11: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 11

Palindromes

• A word that is the same backwards and forwards

• Examples of palindromes:– Mom– Noon– Dad– Kayak– Racecar

• See TestPalindrome.java

Page 12: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 12

Primitive wrapper classes

• Java provides wrapper classes for the primitive types so they can be treated like objects

• All contained in the java.lang package• Helps process primitive values• Will go into more detail with other

primitive wrapper classes in Chapter 9

Page 13: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 13

The Character class

• One constructor, more than 30 methods• Most methods are static• Create a Character object

Character character = new Character(‘a’);

• Return a Character objectcharValue(‘a’);

• Compare Character objectscharacter.compareTo(‘a’);character.equals(‘a’);

Page 14: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 14

The StringBuffer class

• More flexible than String• Can add, insert, or append new

contents• Three constructors, over thirty methods• See Figure 7.8 on p.270 for common

methods

Page 15: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 15

Constructing StringBuffer

• Three constructors– public StringBuffer() – No characters,

initial capacity of sixteen characters– public StringBuffer(int length) – No

characters, initial capacity of length characters

– public StringBuffer(String s) – Constructs a string buffer with an initial capacity of sixteen plus the length of the string argument

Page 16: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 16

Modifying StringBuffers

• Can append new contents to the end of an existing buffer, insert new contents, or delete/replace characters

• Append charactersStringBuffer sb = new StringBuffer(“We”);sb.append(“lcome to Java”);

• Insert charactersStringBuffer sb = new StringBuffer(“As”);sb.insert(1,“ Welcome to Java”);

A Welcome to Javas

Page 17: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 17

Modifying StringBuffers

• Other useful methods

sb.delete(8, 11); // Welcome Javasb.deleteCharAt(8); // Welcome o Javasb.reverse() // avaJ ot emocleWsb.replace(11,15,”HTML”); // Welcome to HTML

sb.setCharAt(0,’w’); // welcome to Java

Page 18: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 18

Modifying StringBuffers• Other useful methods

– toString() – returns the string– capacity() – returns the capacity– length() – returns the number of

characters stored– setLength(newLength) – sets the length,

truncates or pads– charAt(index) – returns the character at

specified index (0-based)• See PalindromeIgnoreNonAlphanumeric.java

Page 19: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 19

The StringTokenizer class

• Allows you to process strings• Specify a set of delimiters• Each string “piece” is a token• Specify delimiters in constructors

Page 20: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 20

Constructing StringTokenizers

• public StringTokenizer(String s, String delim, boolean returnDelims); – delimiters are counted as tokens

• public StringTokenizer(String s, String delim); – delimiters are not counted as tokens

• public StringTokenizer(String s); – default delimiters (“ \t\n\r”) are not counted as tokens

Page 21: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 21

Using StringTokenizers

• countTokens() – Return the number of tokens in string

• hasMoreTokens() – Tells whether the string has more tokens or not

• nextToken() – return next token

Page 22: Strings Chapter 7 CSCI 1302. CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;

CSCI 1302 – Strings 22

The Scanner class

• Can use words as the delimiter• Should be used when words, not single

characters or several single characters are delimiters

• Can parse primitive types• See TestScanner.java