Strings - Java and the Web - Lecture Slides, Slides of Java Programming

During the course study of Java and the Web, I study the main concept about the different programming languages, specially java and the application of the java on the web. In these slides the main key points which I focused during my preparation are: Strings, Fundamental Part, Computing, Data Structure, Series of Characters, Implemented, Strings, Character Array, Strings in Java, Unchangeable

Typology: Slides

2012/2013

Uploaded on 04/23/2013

saravati
saravati 🇮🇳

4.4

(29)

162 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Strings
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Strings - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Strings

Strings

  • Strings are fundamental part of all computing languages.
  • At the basic level, they are just a data structure that can hold a series of characters
  • However, strings are not implemented as a character array in Java as in other languages.

Basic String Methods

  • length() returns the length of the string
  • toLowerCase() converts the string to lower case
  • toUpperCase() converts the string to upper case
  • replace(char, char) replaces occurrences of one character with another character

Basic Strings continued

  • Basic strings are not meant to change frequently so there are no add or append methods
  • However the concat(String) method does allow two strings to be concatenated together

Searching a string

  • Methods for searching strings
    • indexOf( x ) searches for the first occurrence of x
    • indexOf( x , y ) searches for the first occurrence of x after the offset of y
    • lastIndexOf( x ) searches backwards for the first occurrence of x
    • lastIndexOf( x , y ) searches backwards for the first occurrence of x after the offset of y

Example of string search

  • indexOf( x ) and indexOf( x , y ) can find all occurrences of a character(s) in a string public void paint(Graphics g) { String str = new String("Wish You Were Here"); int count = 0; int fromIndex = 0; while(fromIndex != -1) { fromIndex = str.indexOf("er", fromIndex); if (fromIndex != -1) { count++; fromIndex++; } } g.drawString(String.valueOf(count), 10, 10); }

Parsing Strings continued

  • Different default constructors are provided
    • Tokenize the string based on the default delimiters
    • Tokenize the string based on a specified set of delimiters
    • Tokenize the string based on a specified set of delimiters with a boolean flag to specify whether the delimiters should also be returned as tokens

StringBuffer class

  • The StringBuffer class is provided for strings that need may need to be changed
  • The StringBuffer class contains methods for both inserting and appending text
  • An object created as a StringBuffer can easily be converted to an object of the String class if needed

More on StringBuffer Class

  • StringBuffer objects are mutable and capacity & length affect performance
  • If the StringBuffer object needs to be expanded during an append or insert, a new array is created and the old data copied to it
  • Use capacity() and ensureCapacity(int) methods to minimize expansions

Length v. Capacity

  • The length() method returns the length of the string in the StringBuffer
  • The capacity() method returns the total “space” in a StringBuffer
  • The ensureCapacity(int) method insures the StringBuffer has at least the specified amount of capacity remaining