Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连续删除字符串中的第一个单词,并保留最后一个单词[Xamarin Forms]C#_C#_String_Xamarin_Xamarin.forms_Speech Recognition - Fatal编程技术网

连续删除字符串中的第一个单词,并保留最后一个单词[Xamarin Forms]C#

连续删除字符串中的第一个单词,并保留最后一个单词[Xamarin Forms]C#,c#,string,xamarin,xamarin.forms,speech-recognition,C#,String,Xamarin,Xamarin.forms,Speech Recognition,我有一个函数,它将接受一个字符串,删除它的第一个单词,并始终保留最后一个单词 该字符串从my函数SFSpeechRecognitionResult返回 在我当前的代码中,当代码运行一次时,第一个单词会从字符串中删除,只剩下最后一个单词。但是,当函数再次运行时,新添加的单词会一直堆积在result.BestTranscription.FormattedString字符串中,并且第一个单词不会被删除 这是我的职责: RecognitionTask = SpeechRecognizer.GetReco

我有一个函数,它将接受一个
字符串
,删除它的第一个单词,并始终保留最后一个单词

该字符串从my函数
SFSpeechRecognitionResult
返回

在我当前的代码中,当代码运行一次时,第一个单词会从字符串中删除,只剩下最后一个单词。但是,当函数再次运行时,新添加的单词会一直堆积在
result.BestTranscription.FormattedString
字符串中,并且第一个单词不会被删除

这是我的职责:

RecognitionTask = SpeechRecognizer.GetRecognitionTask
(
    LiveSpeechRequest, 
    (SFSpeechRecognitionResult result, NSError err) =>
    {
        if (result.BestTranscription.FormattedString.Contains(" "))
        {
            //and this is where I try to remove the first word and keep the last 
            string[] values = result.BestTranscription.FormattedString.Split(' ');
            var words = values.Skip(1).ToList(); 
            StringBuilder sb = new StringBuilder();
            foreach (var word in words)
            {
                sb.Append(word + " ");
            }

            string newresult = sb.ToString();
            System.Diagnostics.Debug.WriteLine(newresult);
        }
        else 
        {
            //if the string only has one word then I will run this normally
            thetextresult = result.BestTranscription.FormattedString.ToLower();
            System.Diagnostics.Debug.WriteLine(thetextresult);
        }
    }
);

我建议只使用拆分后的最后一个元素:

string last_word = result.BestTranscription.FormattedString.Split(' ').Last();
这将永远给你最后的决定权

确保
result.BestTranscription.FormattedString!=在拆分之前为null
,否则会出现异常

可能还有一个选项,可以在第一次处理后清除字符串,以便始终只获取最后录制的单词。您可以尝试在最后按如下方式重置它:

result.BestTranscription.FormattedString = "";
if (result.BestTranscription.FormattedString != null && 
    result.BestTranscription.FormattedString.Contains(" "))
{
    //and this is where I try to remove the first word and keep the last 
    string lastWord = result.BestTranscription.FormattedString.Split(' ')Last();

    string newresult = lastWord;
    System.Diagnostics.Debug.WriteLine(newresult);
}
基本上,您的代码如下所示:

result.BestTranscription.FormattedString = "";
if (result.BestTranscription.FormattedString != null && 
    result.BestTranscription.FormattedString.Contains(" "))
{
    //and this is where I try to remove the first word and keep the last 
    string lastWord = result.BestTranscription.FormattedString.Split(' ')Last();

    string newresult = lastWord;
    System.Diagnostics.Debug.WriteLine(newresult);
}

这可能是因为您总是在字符串的末尾追加一个空格,因此
.Contains(“”)
是否始终为真?请尝试
String.Join(“,words)
。为什么要将其作为字符串而不是单个单词的
列表?或者甚至可能是一个
队列
,因为它似乎就是这样工作的?一个更快的方法是
string newresult=previousresult.Substring(previousresult.IndexOf(“”))如果我这样做:
如果(howmanyTimesUsed==0){howmanyTimesUsed=howmanyTimesUsed+1;thetextresult=result.BestTranscription.FormattedString.ToLower();resultCallback(thetextresult);}否则{string newresult=result.BestTranscription.FormattedString.Substring(result.BestTranscription.FormattedString.IndexOf(“”);resultCallback(newresult);}
字符串仍然不断添加单词而没有删除注意到我得到了很多反对票:(是因为我没有显示足够的代码吗?@CarlosRodrigez很高兴我能提供帮助。