C#中實(shí)現(xiàn)查找字符串中指定字符位置方法小結(jié)
在字符串中查找“.”字符,并獲得其位置,有很多方法,這些方法也適合查找其它字符:
1.使用IndexOf('.')
在C#中,Text.IndexOf('.')是一種使用IndexOf方法查找字符串中.字符位置的方法。IndexOf方法返回字符在字符串中首次出現(xiàn)的索引位置。如果字符不在字符串中,則返回-1。
以下是C#中Text.IndexOf('.')的用法示例:
// 使用IndexOf在字符串中查找"."并指出其位置
namespace SearchChar
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string text = "Hello. World!";
int index = text.IndexOf('.');
Console.WriteLine(text+"中首次出現(xiàn)符號(hào).的索引= {0}", index.ToString());
}
}
}
//運(yùn)行結(jié)果:
/*
Hello. World!中首次出現(xiàn)符號(hào).的索引= 5
*/2.使用Text.LastIndexOf('.')
此方法返回字符串中最后一個(gè) . 字符的索引。如果字符不在字符串中,則返回 -1。
// 使用LastIndexOf在字符串中查找"."并指出其位置
namespace LastIndexOf
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string text = "Hello, World!";
int index = text.LastIndexOf('.');
Console.WriteLine(text + "中最后出現(xiàn)符號(hào).的索引= {0}", index.ToString());
}
}
}
//運(yùn)行結(jié)果:
/*
Hello. World!中首次出現(xiàn)符號(hào).的索引= 5
*/3.使用Text.IndexOfAny(new char[] { '.', '?' })
此方法返回字符串中 . 或 ? 字符首次出現(xiàn)的索引。如果字符不在字符串中,則返回 -1。
// 使用Text.IndexOfAny(new char[] { '.', '?' })
// 在字符串中查找"."并指出其位置
namespace IndexOfAny
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string text = "Hello. World.";
int index = text.IndexOfAny(['.', '?']);
Console.WriteLine(text + "中首次出現(xiàn)符號(hào).的索引= {0}", index.ToString());
}
}
}
//運(yùn)行結(jié)果:
/*
Hello. World.中首次出現(xiàn)符號(hào).的索引= 5
*/4.使用Text.Substring(0, Text.IndexOf("."))
此方法返回字符串中第一個(gè) . 字符之前的部分。
// 使用Substring在字符串中查找"."并指出其位置
namespace Substring
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string text = "Hello. World!";
string substring = text.Substring(0, text.IndexOf('.'));
Console.WriteLine(text + "中首次出現(xiàn)符號(hào).之前的子串= {0}", substring);
Console.WriteLine(text + "中首次出現(xiàn)符號(hào).索引= {0}", substring.Length);
}
}
}
//運(yùn)行結(jié)果:
/*
Hello. World!中首次出現(xiàn)符號(hào).之前的子串= Hello
Hello. World!中首次出現(xiàn)符號(hào).索引= 5
*/5.使用Text.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)
此方法使用 . 字符作為分隔符將字符串拆分為字符串?dāng)?shù)組,并刪除任何空元素。
// 使用Split在字符串中查找"."并指出其位置
namespace Split
{
internal class Program
{
internal static readonly char[] separator = ['.'];
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string text = "Hello. World!";
string[] parts = text.Split(separator, StringSplitOptions.RemoveEmptyEntries);
foreach (string part in parts)
{
Console.WriteLine(part);
}
Console.WriteLine(text + "中首次出現(xiàn)符號(hào).索引= {0}", parts[0].Length);
}
}
}
//運(yùn)行結(jié)果:
/*
Hello
World!
Hello. World!中首次出現(xiàn)符號(hào).索引= 5
*/6.使用正則表達(dá)式Regex.Match(Text, @"\.")
此方法使用正則表達(dá)式查找字符串中的第一個(gè)子字符串,該子字符串以 . 字符開頭,后跟一個(gè)或多個(gè)字母數(shù)字字符或連字符。
// 使用正則表達(dá)式在字符串中查找'.'
using System.Text.RegularExpressions;
namespace _Regex
{
internal partial class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string text = "Hello. World!";
Regex regex = MyRegex1();
Match match = regex.Match(text);
if (match.Success)
{
int dotIndex = match.Index;
Console.WriteLine(text + "中首次出現(xiàn)符號(hào).之后的子串= {0}", text.Substring(dotIndex + 1));
Console.WriteLine(text + "中首次出現(xiàn)符號(hào).索引= {0}", dotIndex);
}
}
[GeneratedRegex(@"\.")]
private static partial Regex MyRegex1();
}
}
//運(yùn)行結(jié)果:
/*
Hello. World!中首次出現(xiàn)符號(hào).之后的子串= World!
Hello. World!中首次出現(xiàn)符號(hào).索引= 5
*/到此這篇關(guān)于C#中實(shí)現(xiàn)查找字符串中指定字符位置方法小結(jié)的文章就介紹到這了,更多相關(guān)C#查找指定字符位置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)的簡單整數(shù)四則運(yùn)算計(jì)算器功能示例
這篇文章主要介紹了C#實(shí)現(xiàn)的簡單整數(shù)四則運(yùn)算計(jì)算器功能,涉及C#界面布局、事件響應(yīng)及數(shù)值運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
C# .NET實(shí)現(xiàn)Excel到JSON的自動(dòng)化轉(zhuǎn)換
在 .NET 開發(fā)中,Excel 與 JSON 的格式轉(zhuǎn)換是數(shù)據(jù)交互的高頻需求,本文將詳細(xì)講解如何使用免費(fèi) 庫快速實(shí)現(xiàn) Excel 到 JSON 的自動(dòng)化轉(zhuǎn)換,有需要的可以了解下2026-01-01
C# 使用Dictionary復(fù)制克隆副本及比較是否相等
這篇文章主要介紹了C# 使用Dictionary復(fù)制克隆副本及比較是否相等,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
ScriptControl控件執(zhí)行自定義VBS腳本示例分析
這篇文章主要介紹ScriptControl控件 msscript.ocx msscript.oca執(zhí)行自定義VBS腳本的示例代碼,需要的朋友可以參考下2013-04-04
深入了解C#設(shè)計(jì)模式之訂閱發(fā)布模式
這篇文章主要介紹了C#設(shè)計(jì)模式之訂閱發(fā)布模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
C#字節(jié)數(shù)組(byte[])和字符串相互轉(zhuǎn)換方式
這篇文章主要介紹了C#字節(jié)數(shù)組(byte[])和字符串相互轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

