Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java String Methods and Operations, Slides of Network security

An overview of various methods and operations related to strings in java. Topics covered include creating string objects, testing strings for equality, comparing strings, locating things in strings, extracting substrings, changing strings, and other string methods. The document also includes examples of string manipulation using the docsity.com platform.

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

106 documents

1 / 51

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objectives
utilize some useful Java libraries
e.g. String, Scanner, HashMap, and Random
6. Using Libraries
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33

Partial preview of the text

Download Java String Methods and Operations and more Slides Network security in PDF only on Docsity!

  • Objectives
    • utilize some useful Java libraries
      • e.g. String, Scanner, HashMap, and Random

6. Using Libraries

Topics

1. The String Class

2. A Technical Support System

3. The InputReader Class

4. Reading Input with Scanner

5. Designing the Responder Class

6. Maps

7. Making Random Numbers

8. The Responder Class

9. Writing Class Docs for Users

Creating a String Object

String color = "blue";

String s1 = new String("hello ");

char chs[] = {‘a’, ‘n’, ‘d’, ‘y’}; String s2 = new String(chs);

String s3 = s1 + s2 + " davison"; // + is string concatenation

Four different ways (there are more). 1

2

"hello "

s

Testing Strings for Equality

  • s1.equals(s2)
    • lexicographical (dictionary) comparison
    • returns true if s1 and s2 contain the same text
  • s1 == s
    • returns true if s1 and s2 refer to the same object

• Strings should always be compared with

equals().

continued Docsity.com

Comparing Strings

  • s1.compareTo(s2)
    • returns 0 if s1 and s2 are equal
    • returns < 0 if s1 < s2; > 0 if s1 > s
  • s1.startsWith("text")
    • returns true if s1 starts with “text”
  • s1.endsWith("text")
    • returns true if s1 ends with “text”

Locating Things in Strings

  • s1.indexOf('c')
    • returns index position of first ‘c’ in s1, otherwise -
  • s1.lastIndexOf('c')
    • returns index position of last ‘c’ in s1, otherwise -

• Both of these can also take string arguments:

  • s1.indexOf("text")

for text analysis

Changing Strings

  • s1.replace('a', 'd')
    • return new String object; replace every ‘a’ by ‘d’
  • s1.toLowerCase()
    • return new String object where every char has

been converted to lowercase

  • s1.trim()
    • return new String object where any white space

before or after the s1 text has been removed

How do you Change a String?

• Any change to a String object creates a new

object, but this can be assigned back to the

existing String variable.

String w = "foo"; String newW = w + "bar"; w = newW;

or

String w = "foo"; w = w + "bar";

"foo"

w

Strings and Arrays

String[] msgs = new String[2];

msgs[0] = "hello";

msgs[1] = new String("hi");

String t = msgs[1];

t.toLowerCase();

msgs[1].toLowerCase();

t = msgs[1].toLowerCase();

What is built?

What is changed?

2. A Technical Support System

• A technical support system

  • users can describe their software problems and

get advice instantly!

• It read input from the user a line at a time,

and generates 'suitable' responses.

• The idea is based on the Eliza system,

developed by Joseph Weizenbaum.

What Classes and Operations?

• Classes:

  • a top-level class: SupportSystem
    • it will execute a input-response loop
  • a class for obtaining input: InputReader
  • a class for generating responses: Responder

Input

Processing

Response

InputReader

Responder

SupportSystem

  • Operations
    • SupportSystem needs to ask the InputReader for

the user's input line.

  • SupportSystem will have to pass the input line to

the Responder to generate a response.

Information Hiding

• Know what an object can do,

not how it does it.

• Information hiding increases the level of

class independence.

• Class independence simplifies

the building of large systems

and their maintenance.

SupportSystem Processing Loop

printWelcome();

String line, response boolean finished = false; while (!finished) { line = inputReader.getInput( ); if (line.startsWith("bye")) // time to stop finished = true; else { response = responder.genResponse (line) System.out.println( response ); } } printGoodbye();

Input

Processing and Response

InputReader

Responder