Freigeben über


String Class

Definition

The String class represents character strings.

[Android.Runtime.Register("java/lang/String", DoNotGenerateAcw=true)]
public sealed class String : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ISerializable, Java.Lang.ICharSequence, Java.Lang.IComparable, System.Collections.Generic.IEnumerable<char>
[<Android.Runtime.Register("java/lang/String", DoNotGenerateAcw=true)>]
type String = class
    inherit Object
    interface ISerializable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface ICharSequence
    interface seq<char>
    interface IEnumerable
    interface IComparable
Inheritance
String
Attributes
Implements

Remarks

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example: <blockquote>

String str = "abc";

</blockquote>

is equivalent to: <blockquote>

char data[] = {'a', 'b', 'c'};
                String str = new String(data);

</blockquote>

Here are some more examples of how strings can be used: <blockquote>

System.out.println("abc");
                String cde = "cde";
                System.out.println("abc" + cde);
                String c = "abc".substring(2,3);
                String d = cde.substring(1, 2);

</blockquote>

The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the java.lang.Character Character class.

The Java language provides special support for the string concatenation operator (&nbsp;+&nbsp;), and for conversion of other objects to strings. For additional information on string concatenation and conversion, see The Java&trade; Language Specification.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

A String represents a string in the UTF-16 format in which <em>supplementary characters</em> are represented by <em>surrogate pairs</em> (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String.

The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values).

Unless otherwise noted, methods for comparing Strings do not take locale into account. The java.text.Collator class provides methods for finer-grain, locale-sensitive String comparison.

Added in 1.0.

Java documentation for java.lang.String.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

String()

Creates an empty string.

String(Byte[], Charset)

Constructs a new String by decoding the specified array of bytes using the specified java.

String(Byte[], Int32, Int32, Charset)

Constructs a new String by decoding the specified subarray of bytes using the specified java.

String(Byte[], Int32, Int32, Int32)
Obsolete.

Allocates a new String constructed from a subarray of an array of 8-bit integer values.

String(Byte[], Int32, Int32, String)

Constructs a new String by decoding the specified subarray of bytes using the specified charset.

String(Byte[], Int32, Int32)

Constructs a new String by decoding the specified subarray of bytes using the platform's default charset.

String(Byte[], Int32)
Obsolete.

Allocates a new String containing characters constructed from an array of 8-bit integer values.

String(Byte[], String)

Constructs a new String by decoding the specified array of bytes using the specified java.

String(Byte[])

Constructs a new String by decoding the specified array of bytes using the platform's default charset.

String(Char[], Int32, Int32)

Initializes this string to contain the given chars.

String(Char[])

Initializes this string to contain the given chars.

String(Int32[], Int32, Int32)

Allocates a new String that contains characters from a subarray of the Unicode code point array argument.

String(String)

Constructs a new string with the same sequence of characters as toCopy.

String(StringBuffer)

Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.

String(StringBuilder)

Allocates a new string that contains the sequence of characters currently contained in the string builder argument.

Properties

CaseInsensitiveOrder

A Comparator that orders String objects as by compareToIgnoreCase.

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsBlank

Returns true if the string is empty or contains only Character#isWhitespace(int) white space codepoints, otherwise false.

IsEmpty

Returns true if, and only if, #length() is 0.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Object)
ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Object)

Methods

CharAt(Int32)

Returns the char value at the specified index.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
CodePointAt(Int32)

Returns the character (Unicode code point) at the specified index.

CodePointBefore(Int32)

Returns the character (Unicode code point) before the specified index.

CodePointCount(Int32, Int32)

Returns the number of Unicode code points in the specified text range of this String.

CompareTo(String)

Compares two strings lexicographically.

CompareToIgnoreCase(String)

Compares two strings lexicographically, ignoring case differences.

Concat(String)

Concatenates the specified string to the end of this string.

Contains(ICharSequence)

Returns true if and only if this string contains the specified sequence of char values.

Contains(String)

Returns true if and only if this string contains the specified sequence of char values.

ContentEquals(ICharSequence)

Compares this string to the specified CharSequence.

ContentEquals(String)

Compares this string to the specified CharSequence.

ContentEquals(StringBuffer)

Compares this string to the specified StringBuffer.

CopyValueOf(Char[], Int32, Int32)

Equivalent to #valueOf(char[], int, int).

CopyValueOf(Char[])

Equivalent to #valueOf(char[]).

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
EndsWith(String)

Tests if this string ends with the specified suffix.

Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
EqualsIgnoreCase(String)

Compares this String to another String, ignoring case considerations.

Format(Locale, String, Object[])

Returns a formatted string using the specified format string and arguments.

Format(String, Object[])

Returns a formatted string using the specified format string and arguments.

Formatted(Object[])
GetBytes()

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

GetBytes(Charset)

Encodes this String into a sequence of bytes using the given java.

GetBytes(Int32, Int32, Byte[], Int32)
Obsolete.

Copies characters from this string into the destination byte array.

GetBytes(String)

Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

GetChars(Int32, Int32, Char[], Int32)

Copies characters from this string into the destination character array.

GetEnumerator()
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
Indent(Int32)

Adjusts the indentation of each line of this string based on the value of n, and normalizes line termination characters.

IndexOf(Int32, Int32)

Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

IndexOf(Int32)

Returns the index within this string of the first occurrence of the specified character.

IndexOf(String, Int32)

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

IndexOf(String)

Returns the index within this string of the first occurrence of the specified substring.

Intern()

Returns a canonical representation for the string object.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Join(ICharSequence, ICharSequence[])

Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

Join(ICharSequence, IIterable)

Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

Join(String, IIterable)

Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

Join(String, String[])

Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

LastIndexOf(Int32, Int32)

Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.

LastIndexOf(Int32)

Returns the index within this string of the last occurrence of the specified character.

LastIndexOf(String, Int32)

Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.

LastIndexOf(String)

Returns the index within this string of the last occurrence of the specified substring.

Length()

Returns the length of this string.

Matches(String)

Tells whether or not this string matches the given regular expression.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
OffsetByCodePoints(Int32, Int32)

Returns the index within this String that is offset from the given index by codePointOffset code points.

RegionMatches(Boolean, Int32, String, Int32, Int32)

Tests if two string regions are equal.

RegionMatches(Int32, String, Int32, Int32)

Tests if two string regions are equal.

Repeat(Int32)

Returns a string whose value is the concatenation of this string repeated count times.

Replace(Char, Char)

Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

Replace(ICharSequence, ICharSequence)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

Replace(String, String)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

ReplaceAll(String, String)

Replaces each substring of this string that matches the given regular expression with the given replacement.

ReplaceFirst(String, String)

Replaces the first substring of this string that matches the given regular expression with the given replacement.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
Split(String, Int32)

Splits this string around matches of the given regular expression.

Split(String)

Splits this string around matches of the given regular expression.

StartsWith(String, Int32)

Tests if the substring of this string beginning at the specified index starts with the specified prefix.

StartsWith(String)

Tests if this string starts with the specified prefix.

Strip()

Returns a string whose value is this string, with all leading and trailing Character#isWhitespace(int) white space removed.

StripIndent()

Returns a string whose value is this string, with incidental Character#isWhitespace(int) white space removed from the beginning and end of every line.

StripLeading()

Returns a string whose value is this string, with all leading Character#isWhitespace(int) white space removed.

StripTrailing()

Returns a string whose value is this string, with all trailing Character#isWhitespace(int) white space removed.

SubSequence(Int32, Int32)

Returns a character sequence that is a subsequence of this sequence.

SubSequenceFormatted(Int32, Int32)

Returns a character sequence that is a subsequence of this sequence.

Substring(Int32, Int32)

Returns a string that is a substring of this string.

Substring(Int32)

Returns a string that is a substring of this string.

ToArray<T>() (Inherited from Object)
ToCharArray()

Converts this string to a new character array.

ToLowerCase()

Converts all of the characters in this String to lower case using the rules of the default locale.

ToLowerCase(Locale)

Converts all of the characters in this String to lower case using the rules of the given Locale.

ToString()
ToUpperCase()

Converts all of the characters in this String to upper case using the rules of the default locale.

ToUpperCase(Locale)

Converts all of the characters in this String to upper case using the rules of the given Locale.

Transform(IFunction)

This method allows the application of a function to this string.

TranslateEscapes()

Returns a string whose value is this string, with escape sequences translated as if in a string literal.

Trim()

Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).

UnregisterFromRuntime() (Inherited from Object)
ValueOf(Boolean)

Returns the string representation of the boolean argument.

ValueOf(Char)

Returns the string representation of the char argument.

ValueOf(Char[], Int32, Int32)

Returns the string representation of a specific subarray of the char array argument.

ValueOf(Char[])

Returns the string representation of the char array argument.

ValueOf(Double)

Returns the string representation of the double argument.

ValueOf(Int32)

Returns the string representation of the int argument.

ValueOf(Int64)

Returns the string representation of the long argument.

ValueOf(Object)

Returns the string representation of the Object argument.

ValueOf(Single)

Returns the string representation of the float argument.

Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IComparable.CompareTo(Object)
IEnumerable.GetEnumerator()
IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)
SubSequence(ICharSequence, Int32, Int32)

Applies to