本文整理汇总了C#中System.String.LastIndexOf方法的典型用法代码示例。如果您正苦于以下问题:C# String.LastIndexOf方法的具体用法?C# String.LastIndexOf怎么用?C# String.LastIndexOf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.LastIndexOf方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
// This code example demonstrates the
// System.String.LastIndexOf(String, ..., StringComparison) methods.
using System;
using System.Threading;
using System.Globalization;
class Sample
{
public static void Main()
{
string intro = "Find the last occurrence of a character using different " +
"values of StringComparison.";
string resultFmt = "Comparison: {0,-28} Location: {1,3}";
// Define a string to search for.
// U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
string CapitalAWithRing = "\u00c5";
// Define a string to search.
// The result of combining the characters LATIN SMALL LETTER A and COMBINING
// RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character
// LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
string cat = "A Cheshire c" + "\u0061\u030a" + "t";
int loc = 0;
StringComparison[] scValues = {
StringComparison.CurrentCulture,
StringComparison.CurrentCultureIgnoreCase,
StringComparison.InvariantCulture,
StringComparison.InvariantCultureIgnoreCase,
StringComparison.Ordinal,
StringComparison.OrdinalIgnoreCase };
// Clear the screen and display an introduction.
Console.Clear();
Console.WriteLine(intro);
// Display the current culture because culture affects the result. For example,
// try this code example with the "sv-SE" (Swedish-Sweden) culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Console.WriteLine("The current culture is \"{0}\" - {1}.",
Thread.CurrentThread.CurrentCulture.Name,
Thread.CurrentThread.CurrentCulture.DisplayName);
// Display the string to search for and the string to search.
Console.WriteLine("Search for the string \"{0}\" in the string \"{1}\"",
CapitalAWithRing, cat);
Console.WriteLine();
// Note that in each of the following searches, we look for
// LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains
// LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates
// the string was not found.
// Search using different values of StringComparsion. Specify the start
// index and count.
Console.WriteLine("Part 1: Start index and count are specified.");
foreach (StringComparison sc in scValues)
{
loc = cat.LastIndexOf(CapitalAWithRing, cat.Length-1, cat.Length, sc);
Console.WriteLine(resultFmt, sc, loc);
}
// Search using different values of StringComparsion. Specify the
// start index.
Console.WriteLine("\nPart 2: Start index is specified.");
foreach (StringComparison sc in scValues)
{
loc = cat.LastIndexOf(CapitalAWithRing, cat.Length-1, sc);
Console.WriteLine(resultFmt, sc, loc);
}
// Search using different values of StringComparsion.
Console.WriteLine("\nPart 3: Neither start index nor count is specified.");
foreach (StringComparison sc in scValues)
{
loc = cat.LastIndexOf(CapitalAWithRing, sc);
Console.WriteLine(resultFmt, sc, loc);
}
}
}
/*
Note: This code example was executed on a console whose user interface
culture is "en-US" (English-United States).
This code example produces the following results:
Find the last occurrence of a character using different values of StringComparison.
The current culture is "en-US" - English (United States).
Search for the string "Å" in the string "A Cheshire ca°t"
Part 1: Start index and count are specified.
Comparison: CurrentCulture Location: -1
Comparison: CurrentCultureIgnoreCase Location: 12
Comparison: InvariantCulture Location: -1
Comparison: InvariantCultureIgnoreCase Location: 12
Comparison: Ordinal Location: -1
Comparison: OrdinalIgnoreCase Location: -1
Part 2: Start index is specified.
Comparison: CurrentCulture Location: -1
Comparison: CurrentCultureIgnoreCase Location: 12
Comparison: InvariantCulture Location: -1
Comparison: InvariantCultureIgnoreCase Location: 12
Comparison: Ordinal Location: -1
Comparison: OrdinalIgnoreCase Location: -1
Part 3: Neither start index nor count is specified.
Comparison: CurrentCulture Location: -1
Comparison: CurrentCultureIgnoreCase Location: 12
Comparison: InvariantCulture Location: -1
Comparison: InvariantCultureIgnoreCase Location: 12
Comparison: Ordinal Location: -1
Comparison: OrdinalIgnoreCase Location: -1
*/
示例2: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
string searchString = "\u00ADm";
string s1 = "ani\u00ADmal" ;
string s2 = "animal";
int position;
position = s1.LastIndexOf('m');
if (position >= 0) {
Console.WriteLine(s1.LastIndexOf(searchString, position, StringComparison.CurrentCulture));
Console.WriteLine(s1.LastIndexOf(searchString, position, StringComparison.Ordinal));
}
position = s2.LastIndexOf('m');
if (position >= 0) {
Console.WriteLine(s2.LastIndexOf(searchString, position, StringComparison.CurrentCulture));
Console.WriteLine(s2.LastIndexOf(searchString, position, StringComparison.Ordinal));
}
}
}
输出:
4 3 3 -1
示例3: Main
// Sample for String.LastIndexOf(String, Int32, Int32)
using System;
class Sample {
public static void Main() {
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int count;
int end;
start = str.Length-1;
end = start/2 - 1;
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, end);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");
count = 0;
at = 0;
while((start > -1) && (at > -1))
{
count = start - end; //Count must be within the substring.
at = str.LastIndexOf("he", start, count);
if (at > -1)
{
Console.Write("{0} ", at);
start = at - 1;
}
}
Console.Write("{0}{0}{0}", Environment.NewLine);
}
}
输出:
All occurrences of 'he' from position 66 to 32. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The string 'he' occurs at position(s): 56 45
示例4: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
int position = 0;
string s1 = "ani\u00ADmal";
string s2 = "animal";
// Find the index of the soft hyphen.
position = s1.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s1.LastIndexOf("\u00AD", position, position + 1));
position = s2.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s2.LastIndexOf("\u00AD", position, position + 1));
// Find the index of the soft hyphen followed by "n".
position = s1.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s1.LastIndexOf("\u00ADn", position, position + 1));
position = s2.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s2.LastIndexOf("\u00ADn", position, position + 1));
// Find the index of the soft hyphen followed by "m".
position = s1.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s1.LastIndexOf("\u00ADm", position, position + 1));
position = s2.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s2.LastIndexOf("\u00ADm", position, position + 1));
}
}
输出:
'm' at position 4 4 'm' at position 3 3 'm' at position 4 1 'm' at position 3 1 'm' at position 4 4 'm' at position 3 3
示例5: Main
// Sample for String.LastIndexOf(Char, Int32, Int32)
using System;
class Sample {
public static void Main() {
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int count;
int end;
start = str.Length-1;
end = start/2 - 1;
Console.WriteLine("All occurrences of 't' from position {0} to {1}.", start, end);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The letter 't' occurs at position(s): ");
count = 0;
at = 0;
while((start > -1) && (at > -1))
{
count = start - end; //Count must be within the substring.
at = str.LastIndexOf('t', start, count);
if (at > -1)
{
Console.Write("{0} ", at);
start = at - 1;
}
}
Console.Write("{0}{0}{0}", Environment.NewLine);
}
}
输出:
All occurrences of 't' from position 66 to 32. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The letter 't' occurs at position(s): 64 55 44 41 33
示例6: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
string s1 = "ani\u00ADmal";
string s2 = "animal";
Console.WriteLine("Culture-sensitive comparison:");
// Use culture-sensitive comparison to find the last soft hyphen.
Console.WriteLine(s1.LastIndexOf("\u00AD", StringComparison.CurrentCulture));
Console.WriteLine(s2.LastIndexOf("\u00AD", StringComparison.CurrentCulture));
// Use culture-sensitive comparison to find the last soft hyphen followed by "n".
Console.WriteLine(s1.LastIndexOf("\u00ADn", StringComparison.CurrentCulture));
Console.WriteLine(s2.LastIndexOf("\u00ADn", StringComparison.CurrentCulture));
// Use culture-sensitive comparison to find the last soft hyphen followed by "m".
Console.WriteLine(s1.LastIndexOf("\u00ADm", StringComparison.CurrentCulture));
Console.WriteLine(s2.LastIndexOf("\u00ADm", StringComparison.CurrentCulture));
Console.WriteLine("Ordinal comparison:");
// Use ordinal comparison to find the last soft hyphen.
Console.WriteLine(s1.LastIndexOf("\u00AD", StringComparison.Ordinal));
Console.WriteLine(s2.LastIndexOf("\u00AD", StringComparison.Ordinal));
// Use ordinal comparison to find the last soft hyphen followed by "n".
Console.WriteLine(s1.LastIndexOf("\u00ADn", StringComparison.Ordinal));
Console.WriteLine(s2.LastIndexOf("\u00ADn", StringComparison.Ordinal));
// Use ordinal comparison to find the last soft hyphen followed by "m".
Console.WriteLine(s1.LastIndexOf("\u00ADm", StringComparison.Ordinal));
Console.WriteLine(s2.LastIndexOf("\u00ADm", StringComparison.Ordinal));
}
}
输出:
6 5 1 1 4 3 Ordinal comparison: 3 -1 -1 -1 3 -1
示例7: Main
//引入命名空间
using System;
using System.IO;
public class TestLastIndexOf
{
public static void Main()
{
string filename;
filename = ExtractFilename(@"C:\temp\");
Console.WriteLine("{0}", String.IsNullOrEmpty(filename) ? "<none>" : filename);
filename = ExtractFilename(@"C:\temp\delegate.txt");
Console.WriteLine("{0}", String.IsNullOrEmpty(filename) ? "<none>" : filename);
filename = ExtractFilename("delegate.txt");
Console.WriteLine("{0}", String.IsNullOrEmpty(filename) ? "<none>" : filename);
filename = ExtractFilename(@"C:\temp\notafile.txt");
Console.WriteLine("{0}", String.IsNullOrEmpty(filename) ? "<none>" : filename);
}
public static string ExtractFilename(string filepath)
{
// If path ends with a "\", it's a path only so return String.Empty.
if (filepath.Trim().EndsWith(@"\"))
return String.Empty;
// Determine where last backslash is.
int position = filepath.LastIndexOf('\\');
// If there is no backslash, assume that this is a filename.
if (position == -1)
{
// Determine whether file exists in the current directory.
if (File.Exists(Environment.CurrentDirectory + Path.DirectorySeparatorChar + filepath))
return filepath;
else
return String.Empty;
}
else
{
// Determine whether file exists using filepath.
if (File.Exists(filepath))
// Return filename without file path.
return filepath.Substring(position + 1);
else
return String.Empty;
}
}
}
示例8: Main
// Sample for String.LastIndexOf(Char, Int32)
using System;
class Sample {
public static void Main() {
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
start = str.Length-1;
Console.WriteLine("All occurrences of 't' from position {0} to 0.", start);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The letter 't' occurs at position(s): ");
at = 0;
while((start > -1) && (at > -1))
{
at = str.LastIndexOf('t', start);
if (at > -1)
{
Console.Write("{0} ", at);
start = at - 1;
}
}
Console.Write("{0}{0}{0}", Environment.NewLine);
}
}
输出:
All occurrences of 't' from position 66 to 0. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The letter 't' occurs at position(s): 64 55 44 41 33 11 7
示例9: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
string[] strSource = { "<b>This is bold text</b>", "<H1>This is large Text</H1>",
"<b><i><font color=green>This has multiple tags</font></i></b>",
"<b>This has <i>embedded</i> tags.</b>",
"This line ends with a greater than symbol and should not be modified>" };
// Strip HTML start and end tags from each string if they are present.
foreach (string s in strSource)
{
Console.WriteLine("Before: " + s);
string item = s;
// Use EndsWith to find a tag at the end of the line.
if (item.Trim().EndsWith(">"))
{
// Locate the opening tag.
int endTagStartPosition = item.LastIndexOf("</");
// Remove the identified section, if it is valid.
if (endTagStartPosition >= 0 )
item = item.Substring(0, endTagStartPosition);
// Use StartsWith to find the opening tag.
if (item.Trim().StartsWith("<"))
{
// Locate the end of opening tab.
int openTagEndPosition = item.IndexOf(">");
// Remove the identified section, if it is valid.
if (openTagEndPosition >= 0)
item = item.Substring(openTagEndPosition + 1);
}
}
// Display the trimmed string.
Console.WriteLine("After: " + item);
Console.WriteLine();
}
}
}
输出:
Before: This is bold text After: This is bold text Before:This is large Text
After: This is large Text Before: This has multiple tags After: This has multiple tags Before: This has embedded tags. After: This has embedded tags. Before: This line ends with a greater than symbol and should not be modified> After: This line ends with a greater than symbol and should not be modified>
示例10: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
string s1 = "ani\u00ADmal";
string s2 = "animal";
// Find the index of the last soft hyphen.
Console.WriteLine(s1.LastIndexOf("\u00AD"));
Console.WriteLine(s2.LastIndexOf("\u00AD"));
// Find the index of the last soft hyphen followed by "n".
Console.WriteLine(s1.LastIndexOf("\u00ADn"));
Console.WriteLine(s2.LastIndexOf("\u00ADn"));
// Find the index of the last soft hyphen followed by "m".
Console.WriteLine(s1.LastIndexOf("\u00ADm"));
Console.WriteLine(s2.LastIndexOf("\u00ADm"));
}
}
输出:
6 5 1 1 4 3
示例11: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
string searchString = "\u00ADm";
string s1 = "ani\u00ADmal" ;
string s2 = "animal";
int position;
position = s1.LastIndexOf('m');
if (position >= 1) {
Console.WriteLine(s1.LastIndexOf(searchString, position, position, StringComparison.CurrentCulture));
Console.WriteLine(s1.LastIndexOf(searchString, position, position, StringComparison.Ordinal));
}
position = s2.LastIndexOf('m');
if (position >= 1) {
Console.WriteLine(s2.LastIndexOf(searchString, position, position, StringComparison.CurrentCulture));
Console.WriteLine(s2.LastIndexOf(searchString, position, position, StringComparison.Ordinal));
}
}
}
输出:
4 3 3 -1
示例12: Main
// Sample for String.LastIndexOf(String, Int32)
using System;
class Sample {
public static void Main() {
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
start = str.Length-1;
Console.WriteLine("All occurrences of 'he' from position {0} to 0.", start);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");
at = 0;
while((start > -1) && (at > -1))
{
at = str.LastIndexOf("he", start);
if (at > -1)
{
Console.Write("{0} ", at);
start = at - 1;
}
}
Console.Write("{0}{0}{0}", Environment.NewLine);
}
}
输出:
All occurrences of 'he' from position 66 to 0. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The string 'he' occurs at position(s): 56 45 8
示例13: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
int position = 0;
string s1 = "ani\u00ADmal";
string s2 = "animal";
// Find the index of the soft hyphen.
position = s1.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s1.LastIndexOf("\u00AD", position));
position = s2.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s2.LastIndexOf("\u00AD", position));
// Find the index of the soft hyphen followed by "n".
position = s1.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s1.LastIndexOf("\u00ADn", position));
position = s2.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s2.LastIndexOf("\u00ADn", position));
// Find the index of the soft hyphen followed by "m".
position = s1.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s1.LastIndexOf("\u00ADm", position));
position = s2.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s2.LastIndexOf("\u00ADm", position));
}
}
输出:
'm' at position 4 4 'm' at position 3 3 'm' at position 4 1 'm' at position 3 1 'm' at position 4 4 'm' at position 3 3
示例14: Main
//引入命名空间
using System;
public class StringSearchDemo {
public static void Main() {
string str = "C# has powerful string handling.";
int idx;
Console.WriteLine("str: " + str);
idx = str.IndexOf('h');
Console.WriteLine("Index of first 'h': " + idx);
idx = str.LastIndexOf('h');
Console.WriteLine("Index of last 'h': " + idx);
idx = str.IndexOf("ing");
Console.WriteLine("Index of first \"ing\": " + idx);
idx = str.LastIndexOf("ing");
Console.WriteLine("Index of last \"ing\": " + idx);
char[] chrs = { 'a', 'b', 'c' };
idx = str.IndexOfAny(chrs);
Console.WriteLine("Index of first 'a', 'b', or 'c': " + idx);
if(str.StartsWith("C# has"))
Console.WriteLine("str begins with \"C# has\"");
if(str.EndsWith("ling."))
Console.WriteLine("str ends with \"ling.\"");
}
}