Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Strings Chapter 7 CSCI 1302

2 CSCI 1302 – Strings2 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

3 CSCI 1302 – Strings3 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 -4 s1.compareTo(s3); // returns 0 s1.compareTo(s4); // returns 2

4 CSCI 1302 – Strings4 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

5 CSCI 1302 – Strings5 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

6 CSCI 1302 – Strings6 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 found public int indexOf(int ch); public int indexOf(int ch, int from); public int indexOf(String str); public int indexOf(String str, int fromIndex);

7 CSCI 1302 – Strings7 String Conversions Examples "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

8 CSCI 1302 – Strings8 Conversions between Strings and Arrays String to char Array char[] chars = “Java”.toCharArray(); Use getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) to replace substrings char[] dst = {‘J’,’A’,’V’,’A’}; “SAWS”.getChars(2,3,dst,2); dst becomes {‘J’,’A’,’W’,’S’}

9 CSCI 1302 – Strings9 Conversions between Strings and Arrays Convert array of characters into a string String str = new String(new char[]{‘J’,’a’,’v’,’a’}); String str = String.valueOf(new char[]{‘J’,’a’,’v’,’a’});

10 CSCI 1302 – Strings10 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);

11 CSCI 1302 – Strings11 Palindromes A word that is the same backwards and forwards Examples of palindromes: –Mom –Noon –Dad –Kayak –Racecar See TestPalindrome.java

12 CSCI 1302 – Strings12 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

13 CSCI 1302 – Strings13 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 object charValue(‘a’); Compare Character objects character.compareTo(‘a’); character.equals(‘a’);

14 CSCI 1302 – Strings14 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

15 CSCI 1302 – Strings15 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

16 CSCI 1302 – Strings16 Modifying StringBuffers Can append new contents to the end of an existing buffer, insert new contents, or delete/replace characters Append characters StringBuffer sb = new StringBuffer(“We”); sb.append(“lcome to Java”); Insert characters StringBuffer sb = new StringBuffer(“As”); sb.insert(1,“ Welcome to Java”); A Welcome to Javas

17 CSCI 1302 – Strings17 Modifying StringBuffers Other useful methods sb.delete(8, 11); // Welcome Java sb.deleteCharAt(8); // Welcome o Java sb.reverse() // avaJ ot emocleW sb.replace(11,15,”HTML”); // Welcome to HTML sb.setCharAt(0,’w’); // welcome to Java

18 CSCI 1302 – Strings18 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

19 CSCI 1302 – Strings19 The StringTokenizer class Allows you to process strings Specify a set of delimiters Each string “piece” is a token Specify delimiters in constructors

20 CSCI 1302 – Strings20 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

21 CSCI 1302 – Strings21 Using StringTokenizers countTokens() – Return the number of tokens in string hasMoreTokens() – Tells whether the string has more tokens or not nextToken() – return next token

22 CSCI 1302 – Strings22 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


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

Similar presentations


Ads by Google