Advertisement
Methods | Description |
---|---|
LastIndexOf(char ch) | It returns the last index of a char ch found in the String object. |
LastIndexOf(char ch, int index) | It returns the last index of a char value(if found) in the String object. The search starts at a specified index position and proceeds backwards toward the beginning of the invoked String. |
LastIndexOf(String str) | It returns the last index of a String str found in the String object. |
LastIndexOf(String str, int index) | It returns the last index of a String value(if found) in the String object. The search starts at a specified index position and proceeds backwards toward the beginning of the invoked String. |
LastIndexOf(String str, StringComparison) | It returns the last index of a String value(if found) in the String object. The search is performed on the basis of the specified field of type StringComparison. |
// C# Example of LastIndexOf(char)
using System;
class StringIndex1
{
public static void Main()
{
//Creating a String object
String str1 = "Let us decode CSharp!";
//Priting the String object
Console.WriteLine("String: " + str1);
//Calling the LastIndexOf() method
int index = str1.LastIndexOf('e');
Console.WriteLine("Last Index of 'e' in " + str1 + " : "+ index);
//Calling the LastIndexOf() method
index = str1.LastIndexOf('d');
Console.WriteLine("Last Index of 'd' in " + str1 + " : "+ index);
//Calling the LastIndexOf() method
index = str1.LastIndexOf('z');
Console.WriteLine("Last Index of 'z' in " + str1 + " : "+ index);
//Calling the LastIndexOf() method
index = str1.LastIndexOf('x');
Console.WriteLine("Last Index of 'x' in " + str1 + " : "+ index);
//Calling the LastIndexOf() method
index = str1.LastIndexOf('!');
Console.WriteLine("Last Index of '!' in " + str1 + " : "+ index);
}
}
String: Let us decode CSharp!
Last Index of 'e' in Let us decode CSharp! : 12
Last Index of 'd' in Let us decode CSharp! : 11
Last Index of 'z' in Let us decode CSharp! : -1
Last Index of 'x' in Let us decode CSharp! : -1
Last Index of '!' in Let us decode CSharp! : 20
//C# Example of LastIndexOf(char, int)
using System;
class StringIndex1
{
public static void Main()
{
//Creating a String object
String str = "Blue Sky Blue";
//Printing the String object
Console.WriteLine("String: "+ str);
//Calling the LastIndexOf() method
int index = str.LastIndexOf('u', 6);
Console.WriteLine("Last Index of 'u' starting at index 6 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf('u', 12);
Console.WriteLine("Last Index of 'u' starting at index 12 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf('B', 4);
Console.WriteLine("Last Index of 'B' starting at index 4 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf('B', 10);
Console.WriteLine("Last Index of 'B' starting at index 10 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf('S', 3);
Console.WriteLine("Last Index of 'S' starting at index 3 in " + str + " : "+ index);
}
}
String: Blue Sky Blue
Last Index of 'u' starting at index 6 in Blue Sky Blue : 2
Last Index of 'u' starting at index 12 in Blue Sky Blue : 11
Last Index of 'B' starting at index 4 in Blue Sky Blue : 0
Last Index of 'B' starting at index 10 in Blue Sky Blue : 9
Last Index of 'S' starting at index 3 in Blue Sky Blue : -1
//C# Example of LastIndexOf(String)
using System;
class StringIndex1
{
public static void Main()
{
//Creating a String object
String str = "Blue Sky Blue";
//Printing the String object
Console.WriteLine("String: "+ str);
//Calling the LastIndexOf() method
int index = str.LastIndexOf("Blue");
Console.WriteLine("Last Index of Blue in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf("Sky");
Console.WriteLine("Last Index of Sky in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf("Ting");
Console.WriteLine("Last Index of Ting in " + str + " : "+ index);
}
}
String: Blue Sky Blue
Last Index of Blue in Blue Sky Blue : 9
Last Index of Sky in Blue Sky Blue : 5
Last Index of Ting in Blue Sky Blue : -1
Advertisement
//C# Example of LastIndexOf(String, int)
using System;
class StringIndex1
{
public static void Main()
{
//Creating a String object
String str = "Live and let live!";
//Printing the String object
Console.WriteLine("String: "+ str);
//Calling the LastIndexOf() method
int index = str.LastIndexOf("Live", 6);
Console.WriteLine("Last Index of 'Live' starting at index 6 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf("live", 6);
Console.WriteLine("Last Index of 'live' starting at index 6 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf("live", 16);
Console.WriteLine("Last Index of 'live' starting at index 16 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf("and", 10);
Console.WriteLine("Last Index of 'and' starting at index 10 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf("t", 12);
Console.WriteLine("Last Index of 't' starting at index 12 in " + str + " : "+ index);
}
}
String: Live and let live!
Last Index of 'Live' starting at index 6 in Live and let live! : 0
Last Index of 'live' starting at index 6 in Live and let live! : -1
Last Index of 'live' starting at index 16 in Live and let live! : 13
Last Index of 'and' starting at index 10 in Live and let live! : 5
Last Index of 't' starting at index 12 in Live and let live! : 11
Fields of StringComparison | Value | Description |
---|---|---|
CurrentCulture | 0 | Compares String objects using culture-sensitive sort rules and current culture. |
CurrentCultureIgnoreCase | 1 | Compares String objects using culture-sensitive sort rules and current culture, while ignoring the case of the String objects being compared. |
InvariantCulture | 2 | Compares String objects using culture-sensitive sort rules and invariant culture. |
InvariantCultureIgnoreCase | 3 | Compares String objects using culture-sensitive sort rules and invariant culture, while ignoring the case of the String objects being compared. |
Ordinal | 4 | Compares String objects using ordinal(binary) sort rules. |
OrdinalIgnoreCase | 5 | Compares String objects using ordinal(binary) sort rules, while ignoring the case of the String objects being compared. |
//C# Example of LastIndexOf(String, StringComparison) method of String.
using System;
class StringEx
{
public static void Main()
{
String str1= "Live and let live!";
Console.WriteLine("String is: " + str1);
//String comparison made using StringComparison.CurrentCulture
Console.WriteLine("String comparison by StringComparison.CurrentCulture:");
int index = str1.LastIndexOf("ive", StringComparison.CurrentCulture);
Console.WriteLine("The last index of 'ive' in "+ str1 + " is at ? "+ index);
index = str1.LastIndexOf("Live", StringComparison.CurrentCulture);
Console.WriteLine("The last index of 'Live' in "+ str1 + " is at ? "+ index);
//String comparison made using StringComparison.CurrentCultureIgnoreCase
Console.WriteLine("String comparison by StringComparison.CurrentCultureIgnoreCase:");
index = str1.LastIndexOf("LIVE", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine("The last index of 'LIVE' in "+ str1 + " is at ? "+ index);
index = str1.LastIndexOf("LET", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine("The last index of 'LET' in "+ str1 + " is at ? "+ index);
//String comparison made using StringComparison.InvariantCulture
Console.WriteLine("String comparison by StringComparison.InvariantCulture: ");
index = str1.LastIndexOf("ive", StringComparison.InvariantCulture);
Console.WriteLine("The last index of 'ive' in "+ str1 + " is at ? "+ index);
index = str1.LastIndexOf("Live", StringComparison.InvariantCulture);
Console.WriteLine("The last index of 'Live' in "+ str1 + " is at ? "+ index);
//String comparison made using StringComparison.InvariantCultureIgnoreCase
Console.WriteLine("String comparison by StringComparison.InvariantCultureIgnoreCase: ");
index = str1.LastIndexOf("LIVE", StringComparison.InvariantCultureIgnoreCase);
Console.WriteLine("The last index of 'LIVE' in "+ str1 + " is at ? "+ index);
index = str1.LastIndexOf("LET", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine("The last index of 'LET' in "+ str1 + " is at ? "+ index);
//String comparison made using StringComparison.Ordinal
Console.WriteLine("String comparison by StringComparison.Ordinal: ");
index = str1.LastIndexOf("ive", StringComparison.Ordinal);
Console.WriteLine("The last index of 'ive' in "+ str1 + " is at ? "+ index);
index = str1.LastIndexOf("Live", StringComparison.Ordinal);
Console.WriteLine("The last index of 'Live' in "+ str1 + " is at ? "+ index);
//String comparison made using StringComparison.OrdinalIgnoreCase
Console.WriteLine("String comparison by StringComparison.OrdinalIgnoreCase: ");
index = str1.LastIndexOf("LIVE", StringComparison.OrdinalIgnoreCase);
Console.WriteLine("The last index of 'LIVE' in "+ str1 + " is at ? "+ index);
index = str1.LastIndexOf("LET", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine("The last index of 'LET' in "+ str1 + " is at ? "+ index);
}
}
String is: Live and let live!
String comparison by StringComparison.CurrentCulture:
The last index of 'ive' in Live and let live! is at ? 14
The last index of 'Live' in Live and let live! is at ? 0
String comparison by StringComparison.CurrentCultureIgnoreCase:
The last index of 'LIVE' in Live and let live! is at ? 13
The last index of 'LET' in Live and let live! is at ? 9
String comparison by StringComparison.InvariantCulture:
The last index of 'ive' in Live and let live! is at ? 14
The last index of 'Live' in Live and let live! is at ? 0
String comparison by StringComparison.InvariantCultureIgnoreCase:
The last index of 'LIVE' in Live and let live! is at ? 13
The last index of 'LET' in Live and let live! is at ? 9
String comparison by StringComparison.Ordinal:
The last index of 'ive' in Live and let live! is at ? 14
The last index of 'Live' in Live and let live! is at ? 0
String comparison by StringComparison.OrdinalIgnoreCase:
The last index of 'LIVE' in Live and let live! is at ? 13
The last index of 'LET' in Live and let live! is at ? 9
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement