Strings - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Strings

Description:

Keine Methoden zum ndern des String-Inhaltes (c) JKU, Abteilung f r ... static String toString(int i, int radix) - String HexStr = Integer.toString(255, 16) ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 12
Provided by: tkUnil
Category:
Tags: radix | strings

less

Transcript and Presenter's Notes

Title: Strings


1
Strings
2
Strings String 1/2
  • Unicode Zeichenkette
  • Besondere Unterstützung durch den Compiler
  • String-Literale
  • String s "Hello"
  • Der Verkettungsoperator
  • s s ", Jimmy!"
  • Der Operator erzeugt ein neues Objekt!
  • String a, b
  • a "Hello"
  • b a
  • a a " World"

a
"Hello World"
b
"Hello"
3
Strings String 2/2
  • Schnittstelle
  • String() //Leerer String
  • String(String value) //Kopie
  • String(StringBuffer buffer) //aus StringBuffer
  • int length() //Länge des Strings
  • char charAt(int index) //1. Zeichen bei 0
  • String concat(String str) //vgl. Operator
  • static String valueOf(int i)
  • Strings sind unveränderbar
  • Kein direkter Zugriff auf Instanzvariablen
  • Keine Methoden zum Ändern des String-Inhaltes

4
Strings Vergleich von Strings 1/2
  • Identität Vergleich der Objekt-Referenzen
  • boolean identity (ab)
  • Gleichheit Vergleich des Objekt-Inhaltes
  • boolean equality a.equals(b)
  • (a) Vergleich mit dem identischen Objekt
  • String a, b
  • a "Hello"
  • b a
  • a b //true (Zeigervergleich)
  • a.equals(b) //true (Wertevergleich)

a
"Hello"
b
5
Strings Vergleich von Strings 2/2
  • (b) Vergleich mit gleichen Objekten
  • String a, b
  • a "Hello"
  • b new String(a) //Kopie
  • a b //false (Zeigervergleich)
  • a.equals(b) //true (Wertevergleich)
  • Besonderheit bei String-Literalen
  • a "Hello"
  • b "Hello"
  • a b //true

a
"Hello"
b
"Hello"
a
"Hello"
b
6
Strings Konvertierungsmethoden
  • Methoden zum Umwandeln zwischen primitiven
    Datentypen und Strings
  • In den Klassen Boolean, Byte, Short, Integer,
    usw. sowie in der Klasse String (alle im Package
    java.lang)
  • Z.B. in der Schnittstelle von Integer
  • static int parseInt(String str)
  • static String toString(int i)
  • static String toString(int i, int radix)
  • -gt String HexStr Integer.toString(255, 16)

7
Strings String-Methoden
  • char charAt(int index) Returns the character
    at the specified index.
  • int compareTo(Object o) Compares this String to
    another Object.
  • int compareTo(String anotherString) Compares two
    strings lexicographically.
  • int compareToIgnoreCase(String str)Compares two
    strings lexicographically, ignoring case
    differences.

8
Strings String-Methoden
  • String concat(String str)
  • Concatenates the specified string to the end of
    this string.
  • boolean endsWith(String suffix)
  • Tests if this string ends with the specified
    suffix.
  • boolean equals(Object anObject)
  • Compares this string to the specified object.
  • boolean equalsIgnoreCase(String anotherString)
  • Compares this String to another String, ignoring
    case considerations.

9
Strings String-Methoden
  • int indexOf(int ch)
  • Returns the index within this string of the
    first occurrence of the specified character.
  • int indexOf(int ch, int fromIndex)
  • Returns the index within this string of the
    first occurrence of the specified character,
    starting the search at the specified index.
  • int indexOf(String str)
  • Returns the index within this string of the
    first occurrence of the specified substring.
  • int indexOf(String str, int fromIndex)
  • Returns the index within this string of the
    first occurrence of the specified substring,
    starting at the specified index.

10
Strings String-Methoden
  • int length()
  • Returns the length of this string.
  • boolean matches(String regex)
  • Tells whether or not this string matches the
    given regular expression.
  • String replace(char oldChar, char newChar)
  • Returns a new string resulting from replacing
    all occurrences of oldChar in this string with
    newChar.

11
Strings String-Methoden
  • String substring(int beginIndex)
  • Returns a new string that is a substring of this
    string.
  • String substring(int beginIndex, int endIndex)
  • Returns a new string that is a substring of this
    string.
  • String toLowerCase()
  • Converts all of the characters in this String to
    lower case using the rules of the default
    locale.
  • String toUpperCase()
  • Converts all of the characters in this String to
    upper case using the rules of the default
    locale.
  • String trim()
  • Returns a copy of the string, with leading and
    trailing whitespace omitted.

12
Arrays Konvertierung char - String
  • String aus char-Array, z.B.
  • char a new char10
  • for(int i 0 ilta.length i) ai''
  • String s new String(a) //""
  • char-Array aus String
  • char a "Hello".toCharArray()
  • Kopieren von Arrays
  • System.arraycopy(Object src, int src_position,
    Object dst, int dst_position, int length)
  • z.B.
  • char b new char3
  • System.arraycopy(a, 1, b, 0, b.length)
Write a Comment
User Comments (0)
About PowerShow.com