Share via


String.LastIndexOf Method

Definition

Overloads

LastIndexOf(Int32)

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

LastIndexOf(String)

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

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(String, Int32)

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

LastIndexOf(Int32)

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

[Android.Runtime.Register("lastIndexOf", "(I)I", "")]
public int LastIndexOf (int ch);
[<Android.Runtime.Register("lastIndexOf", "(I)I", "")>]
member this.LastIndexOf : int -> int

Parameters

ch
Int32

a character (Unicode code point).

Returns

the index of the last occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

Attributes

Remarks

Returns the index within this string of the last occurrence of the specified character. For values of ch in the range from 0 to 0xFFFF (inclusive), the index (in Unicode code units) returned is the largest value k such that: <blockquote>

this.charAt(<i>k</i>) == ch

</blockquote> is true. For other values of ch, it is the largest value k such that: <blockquote>

this.codePointAt(<i>k</i>) == ch

</blockquote> is true. In either case, if no such character occurs in this string, then -1 is returned. The String is searched backwards starting at the last character.

Java documentation for java.lang.String.lastIndexOf(int).

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.

Applies to

LastIndexOf(String)

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

[Android.Runtime.Register("lastIndexOf", "(Ljava/lang/String;)I", "")]
public int LastIndexOf (string str);
[<Android.Runtime.Register("lastIndexOf", "(Ljava/lang/String;)I", "")>]
member this.LastIndexOf : string -> int

Parameters

str
String

the substring to search for.

Returns

the index of the last occurrence of the specified substring, or -1 if there is no such occurrence.

Attributes

Exceptions

if string is null.

Remarks

Returns the index within this string of the last occurrence of the specified substring. The last occurrence of the empty string "" is considered to occur at the index value this.length().

The returned index is the largest value k for which:

{@code
            this.startsWith(str, k)
            }

If no such value of k exists, then -1 is returned.

Java documentation for java.lang.String.lastIndexOf(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.

Applies to

LastIndexOf(Int32, Int32)

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

[Android.Runtime.Register("lastIndexOf", "(II)I", "")]
public int LastIndexOf (int ch, int fromIndex);
[<Android.Runtime.Register("lastIndexOf", "(II)I", "")>]
member this.LastIndexOf : int * int -> int

Parameters

ch
Int32

a character (Unicode code point).

fromIndex
Int32

the index to start the search from. There is no restriction on the value of fromIndex. If it is greater than or equal to the length of this string, it has the same effect as if it were equal to one less than the length of this string: this entire string may be searched. If it is negative, it has the same effect as if it were -1: -1 is returned.

Returns

the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to fromIndex, or -1 if the character does not occur before that point.

Attributes

Remarks

Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. For values of ch in the range from 0 to 0xFFFF (inclusive), the index returned is the largest value k such that: <blockquote>

(this.charAt(<i>k</i>) == ch) {@code &&} (<i>k</i> &lt;= fromIndex)

</blockquote> is true. For other values of ch, it is the largest value k such that: <blockquote>

(this.codePointAt(<i>k</i>) == ch) {@code &&} (<i>k</i> &lt;= fromIndex)

</blockquote> is true. In either case, if no such character occurs in this string at or before position fromIndex, then -1 is returned.

All indices are specified in char values (Unicode code units).

Java documentation for java.lang.String.lastIndexOf(int, int).

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.

Applies to

LastIndexOf(String, Int32)

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

[Android.Runtime.Register("lastIndexOf", "(Ljava/lang/String;I)I", "")]
public int LastIndexOf (string str, int fromIndex);
[<Android.Runtime.Register("lastIndexOf", "(Ljava/lang/String;I)I", "")>]
member this.LastIndexOf : string * int -> int

Parameters

str
String

the substring to search for.

fromIndex
Int32

the index to start the search from.

Returns

the index of the last occurrence of the specified substring, searching backward from the specified index, or -1 if there is no such occurrence.

Attributes

Exceptions

if subString is null.

Remarks

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

The returned index is the largest value k for which:

{@code
                k <= Math.min(fromIndex, this.length()) &&
                              this.startsWith(str, k)
            }

If no such value of k exists, then -1 is returned.

Java documentation for java.lang.String.lastIndexOf(java.lang.String, int).

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.

Applies to