获取字符串中的最后一个字符

cly*_*nux 5 vb.net windows-phone

我是开发Windows手机应用程序的新手.现在我正在用T9键盘创建一个文本信使应用程序,我已经设计了像按钮一样的设计.现在我想要的是如何获得字符串中的最后一个字符?字符串示例为"clyde",如何从该字符串中获取char'e'?我使用Visual Basic作为语言.

更新:现在工作了,我使用了这段代码:

string s = "clyde";
char e = s(s.Length-1);
Run Code Online (Sandbox Code Playgroud)

not*_*row 8

我不确定你使用的是哪种语言,但在C#中就是这样

string s = "clyde";
char e = s[s.Length-1];
Run Code Online (Sandbox Code Playgroud)

它在每种语言中都非常相似.


Cla*_*sen 7

C#:

string clyde = "clyde"; 
char last = clyde[clyde.Length - 1];
Run Code Online (Sandbox Code Playgroud)

网络

string clyde = "clyde"; 
char last = clyde(clyde.Length - 1);
Run Code Online (Sandbox Code Playgroud)

  • vb.net 示例不是 vb.net (8认同)

Der*_*rek 5

我会用linq做到这一点: -

   string clyde = "Clyde";
   char lastChar = clyde.Last();
Run Code Online (Sandbox Code Playgroud)

只是我的偏好.

  • Enumerable.Last仅针对IList/ICollection <T>类型进行了优化,其中string仅实现[]重载.正如您在http://msdn.microsoft.com/en-us/library/system.string.aspx上看到的那样,它既没有实现IList <T>也没有实现ICollection <T>,因此IEnumerable优化赢得了'工作.因此,它将遍历集合中的所有项目(字符串中的所有字符),然后选择最后一个.它是O(n)操作而不是O(1). (3认同)
  • 供将来参考,您能否解释一下使用LINQ如何效率低下。只是为了让我能绕开它,因为我没有那么经验。 (2认同)