C# C中插入符号位置、字符串长度和匹配索引不一致#

C# C中插入符号位置、字符串长度和匹配索引不一致#,c#,textbox,caret,scintilla,C#,Textbox,Caret,Scintilla,我正在尝试使用正则表达式在闪烁文本框中获取当前选定的单词,我注意到报告的字符串长度、匹配索引和插入符号位置或选择开始之间存在一些不一致: private KeyValuePair<int, string> get_current_word() { int cur_pos = scin_txt.Selection.Start; KeyValuePair<int, string> kvp_word = new KeyValuePair<int, stri

我正在尝试使用正则表达式在闪烁文本框中获取当前选定的单词,我注意到报告的字符串长度、匹配索引和插入符号位置或选择开始之间存在一些不一致:

private KeyValuePair<int, string> get_current_word()
{
    int cur_pos = scin_txt.Selection.Start;
    KeyValuePair<int, string> kvp_word = new KeyValuePair<int, string>(0, "");
    MatchCollection words = Regex.Matches(scin_txt.Text, @"\b(?<word>\w+)\b");
    foreach (Match word in words)
    {
        int start = word.Index;
        int end = start + word.Length;
        if (start <= cur_pos && cur_pos <= end)
        {
            kvp_word = new KeyValuePair<int,string>(start, word.Value);
            break;
        }
    }
    return kvp_word;
}
私钥值对获取当前单词()
{
int cur_pos=scin_txt.Selection.Start;
KeyValuePair kvp_word=新的KeyValuePair(0,“”);
MatchCollection words=Regex.Matches(scin_txt.Text,@“\b(?\w+)\b”);
foreach(在单词中匹配单词)
{
int start=word.Index;
int end=start+word.Length;

如果(开始闪烁API的名称不正确,
Text
属性返回字节,而不是文本,
TextLength
给出字节数,而不是字符数

假设您使用的是UTF-8模式,因此“文本”实际上是:

Le clic droit a\xc3\xa9t\xc3\xa9 d\xc3\xa9sactiv\xc3\xa9 pour cette image.J

正好53字节长

编辑


如果你想找到一个单词的开头/结尾的位置,那么这里有消息。对于插入符号定位,这里有消息,考虑到当前代码页。(可能这些消息在您正在使用的特定闪烁绑定的API中都有功能等价物,或者可能有一些用于访问它们的通用
SendMessage
函数).

啊,这就解释了。我想是这样的,但我认为文本是Unicode的,而不是UTF8,结果没有意义。有没有一种简单的方法可以知道插入符号在字符串中的位置,或者另一种方法可以知道当前单词是什么?@Sylverdrag。不确定简单,但请参阅我更新的答案,了解其中的一些内容options.Thank。事实证明它在.NET中非常简单。我找到了一个“.GetColumn(int)”方法,可以将位置转换为文本位置。