C# 我目前的问题是,我想把字符串分成单个单词和标点符号,但不要';我不知道怎么做
(搜索工具) 目前我有一个很长的字符串,它可以包含几个单词,包括标点符号或括号。然而,我现在的问题是,当我键入像“apple”这样的搜索词时,我会按空格过滤我的文本字符串,所以我只得到单个字符。但是,因为文本可能会说“apple”,所以“apple”可能是一个分裂的词,“apple”,现在我仍然必须将com与它分开,不知何故,有可能用所有特殊字符来实现它吗C# 我目前的问题是,我想把字符串分成单个单词和标点符号,但不要';我不知道怎么做,c#,console-application,C#,Console Application,(搜索工具) 目前我有一个很长的字符串,它可以包含几个单词,包括标点符号或括号。然而,我现在的问题是,当我键入像“apple”这样的搜索词时,我会按空格过滤我的文本字符串,所以我只得到单个字符。但是,因为文本可能会说“apple”,所以“apple”可能是一个分裂的词,“apple”,现在我仍然必须将com与它分开,不知何故,有可能用所有特殊字符来实现它吗 string[] splittedTxt = text.Split(' '); if (decisionForWhole
string[] splittedTxt = text.Split(' ');
if (decisionForWholeWords == true && decisionForSpelling == false)
{
foreach (var item in splittedTxt)
{
if (wordToFind.ToLower() == item.ToLower())
{
Console.BackgroundColor = ConsoleColor.Red;
var cursorPositionTop = Console.CursorTop;
var cursorPositionLeft = Console.CursorLeft;
wordFound = true;
foundWordPositions.Add(new CursorPosition(cursorPositionTop, cursorPositionLeft));
}
Console.Write(item);
if (wordFound) // reset color
{
Console.BackgroundColor = ConsoleColor.Black;
wordFound = false;
}
Console.Write(" ");
}
}
如果使用要拆分的字符构建数组,则可以执行此操作,如下所示:
string mystring = "This is some, bla bla. ble, hey guys";
char[] delimiters = { ',', '.' }; //you can add your delimiters in this array
string[] result = myString.Split(delimiters);
输出将是:
"This is some"
" bla bla"
" ble"
" hey guys"
正则表达式模式
\w+
完全匹配苹果公司的“苹果公司”
一个有用的正则表达式测试仪和参考:但是如果我想再次在控制台上打印彩色文本,我会丢失标点符号。