C# 如何从一个有空格的字符串(文本框)中获取插入符号位置的当前单词并按ENTER键?
如何从包含“”和“\r\n”作为单词分隔符的文本框中获取光标位置的当前单词?类似的操作应该可以:C# 如何从一个有空格的字符串(文本框)中获取插入符号位置的当前单词并按ENTER键?,c#,wpf,C#,Wpf,如何从包含“”和“\r\n”作为单词分隔符的文本框中获取光标位置的当前单词?类似的操作应该可以: var currentWord = textBox.Text.Substring(textBox.SelectionStart, textBox.Text.IndexOf(" ", textBox.SelectionStart)); 我使用此选项选择一个单词: private void selectWord() { int cursorPosition = text
var currentWord = textBox.Text.Substring(textBox.SelectionStart, textBox.Text.IndexOf(" ", textBox.SelectionStart));
我使用此选项选择一个单词:
private void selectWord()
{
int cursorPosition = textBox1.SelectionStart;
int nextSpace = textBox1.Text.IndexOf(' ', cursorPosition);
int selectionStart = 0;
string trimmedString = string.Empty;
if (nextSpace != -1)
{
trimmedString = textBox1.Text.Substring(0, nextSpace);
}
else
{
trimmedString = textBox1.Text;
}
if (trimmedString.LastIndexOf(' ') != -1)
{
selectionStart = 1 + trimmedString.LastIndexOf(' ');
trimmedString = trimmedString.Substring(1 + trimmedString.LastIndexOf(' '));
}
textBox1.SelectionStart = selectionStart;
textBox1.SelectionLength = trimmedString.Length;
}
要获取所选单词,请执行以下操作:
textBox1.SelectedText
我现在有这个问题,下面是我处理这个问题的方法。
TxtLineCodes
是RichTextBox
控件,请查看我在代码中的注释
private void CheckCurrentWord()
{
// current caret position
var currentposition = TxtLineCodes.SelectionStart;
// get line number
var linenumber = TxtLineCodes.GetLineFromCharIndex(currentposition);
// get the first character index of the line
var firstlineindex = currentposition;
if (linenumber == 0)
{
firstlineindex = 0;
}
else
{
while (TxtLineCodes.GetLineFromCharIndex(firstlineindex) == linenumber)
{
firstlineindex--;
}
//fix the last iteration
firstlineindex += 1;
}
// if caret is not in the end of the word discover it
var lastcaretwordindex = currentposition;
if (lastcaretwordindex < TxtLineCodes.Text.Length)
while (lastcaretwordindex < TxtLineCodes.Text.Length && TxtLineCodes.Text.Substring(lastcaretwordindex, 1) != " ")
{
lastcaretwordindex += 1;
}
// get the text of the line (until the cursor position)
var linetext = TxtLineCodes.Text.Substring(firstlineindex, lastcaretwordindex - firstlineindex);
// split all the words in current line
string[] words = linetext.Split(' ');
// the last word must be the current word
System.Diagnostics.Debug.WriteLine("current word: " + words[words.Length - 1]);
// and you can also get the substring indexes of the current word
var currentwordbysubstring = TxtLineCodes.Text.Substring(lastcaretwordindex - words[words.Length - 1].Length, words[words.Length - 1].Length);
var startindex = lastcaretwordindex - words[words.Length - 1].Length;
var lastindex = startindex + words[words.Length - 1].Length-1;
System.Diagnostics.Debug.WriteLine("current word: " + currentwordbysubstring + " and its in index (" + startindex + "," + lastindex + ")");
}
private void CheckCurrentWord()
{
//当前插入符号位置
var currentposition=TxtLineCodes.SelectionStart;
//获取行号
var linenumber=TxtLineCodes.GetLineFromCharIndex(当前位置);
//获取行的第一个字符索引
var firstlineindex=当前位置;
如果(行号==0)
{
firstlineindex=0;
}
其他的
{
while(TxtLineCodes.GetLineFromCharIndex(firstlineindex)=行号)
{
一线索引--;
}
//修复最后一次迭代
firstlineindex+=1;
}
//如果插入符号不在单词的末尾,请发现它
var lastcaretwordindex=当前位置;
if(lastCareTowerIndex