Sie sind auf Seite 1von 22

String in Java

http://java.sun.com/javase/6/docs/api/java/lang/String.html

String Class in Java


The String class represents character strings. All string literals in Java programs, such as aaa", are implemented as instances of this class. Examples:
System.out.println("abc"); String cde = mnp"; System.out.println("abc" + cde); String c = "abc".substring(2,3); String d = cde.substring(1, 2);

String Class in Java


The class String includes methods for examining:
Concatenating two strings Individual characters of the sequence Comparing strings Searching strings Extracting substrings Creating a copy of a string with all characters translated to uppercase or to lowercase

String Methods in Java


Concatenating two strings public String concat(String str)

Concatenates the specified string to the end of this string. If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

String Methods in Java


Concatenating two strings public String concat(String str) Examples: "cares".concat("s") returns "caress "to".concat("get").concat("her") returns "together

String Methods in Java


Examining the character at a given position public char charAt(int index)
Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

String Methods in Java


Examining the character at a given position public char charAt(int index) Examples: "cares". charAt(1) returns a "to".charAt(1) returns o

String Methods in Java


Examining the length of a given position public int length() Examples: "cares". length() returns 5 "to".length() returns 2

String Methods in Java


Examining emptiness of a string public boolean isEmpty() Returns true if, and only if, length() is 0. Examples: "cares". isEmpty() returns false "".isEmpty() returns true

String Methods in Java


Comparing two strings public int compareTo(String anotherString)

Compares two strings lexicographically Returns an integer:


Positive if object > argument Negative if object < argument Zero if object = argument

Examples: "cares.compareTo(area) returns a positive integer cares". compareTo(cares) returns zero

String Methods in Java


Testing the prefix/postfix of string public boolean startsWith(String prefix) public boolean endsWith(String suffix)

Examples: "cares.startsWith(care) returns true cares". startsWith(Care) returns false

Tests if this string begins/ends with the specified prefix/suffix. Note that the result will be true if the argument is the empty string

String Methods in Java


Extracting a substring of a string
public String substring(int beginIndex) public String substring(int beginIndex, int endIndex) Returns a substring of a string that begins at position beginIndex end ends at (end of string)/endIndex Examples: "cares.substring(3) returns es cares". substring(2,3) returns re

String Methods in Java


Converting a non-string data to string

public static String valueOf(datatype b)


datatype: any primitive data type Returns the string representation of data Examples: valueOf(23) returns 23 valueOf(26.89) returns 26.89

String Methods in Java


public String toLowerCase() public String toUpperCase()
Converts all of the characters in this String to lower case Examples: cares.toUpperCase() returns CARES IBM pc.toLowerCase() returns ibm pc

Converting a strings case

String Methods in Java


Removing leading and trailing white spaces public String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.

Examples: cares .trim() returns cares IBM pc .trim() returns IBM pc

String Methods in Java


Replacing a character with another public String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

Examples: "mesquite in your cellar".replace('e', 'o') returns "mosquito in your collar"

String Methods in Java


Replacing a substring with another public String replace(CharSequence target, CharSequence replacement)
Returns a new string resulting from replacing all occurrences of target sequence in this string with replacement string

Examples: "mesquite in your mescellar".replace(mes, bas') returns basquito in your bascollar"

String Methods in Java


Splitting a string public String[] split(String regex, int limit)
Splits this string around matches of the given regular expression.

Parameters: regex - the delimiting regular expression limit - The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times If n is zero then the pattern will be applied as many times as possible Returns: the array of strings computed by splitting this string around matches of the given regular expression

String Methods in Java


Splitting a string public String[] split(String regex)

Example:
String [] tokens = JAVA#HTML#Perl.split(#,0); // splits the string at #s into three strings For (int i=0; i<tokens.length(); i++) system.out.print(tokens[i], ); Displays JAVA HTML Perl

String Methods in Java


Finding a character or a substring in a string public int indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring. If no such value of k exists, then -1 is returned. Parameters: str - the substring to search for. Returns: the index of the first occurrence of the specified substring, or -1 if there is no such occurrence.

String Methods in Java


Finding a character or a substring in a string public int indexOf(String str) Example:
Welcome to Java.indexof (W) returns 0 Welcome to Java.indexof (o) returns 4 Welcome to Java.indexof (come) returns 3 Welcome to Java.indexof (Java) returns 11 Welcome to Java.indexof (java) returns -1

StringBuilder/StringBuffer class

StringBuilder/StringBuffer class is an alternative to the String class It is more flexible than the String class You can
Add Insert Append

New content into a StringBuilder object

http://download.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

Das könnte Ihnen auch gefallen