vb.net 获取字符串的最后 5 个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2813505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:52:03  来源:igfitidea点击:

Get last 5 characters in a string

vb.net

提问by NULL

I want to get the last 5 digits/characters from a string. For example, from "I will be going to school in 2011!", I would like to get "2011!".

我想从字符串中获取最后 5 位数字/字符。例如,从"I will be going to school in 2011!",我想得到"2011!"

Any ideas? I know Visual Basic has Right(string, 5); this didn't work for me and gave me an error.

有任何想法吗?我知道 Visual Basic 有Right(string, 5);这对我不起作用并给了我一个错误。

回答by dcp

str.Substring(str.Length - 5)

回答by Glennular

Error check:

错误检查:

result = str.Substring(Math.Max(0, str.Length - 5))

回答by Nick Gotch

Checks for errors:

检查错误:

Dim result As String = str
If str.Length > 5 Then
    result = str.Substring(str.Length - 5)
End If

回答by Umesh Vora

in VB 2008 (VB 9.0) and later, prefix Right() as Microsoft.VisualBasic.Right(string, number of characters)

在 VB 2008 (VB 9.0) 及更高版本中,前缀 Right() 为 Microsoft.VisualBasic.Right(string, number of characters)

Dim str as String = "Hello World"

Dim str as String = "Hello World"

Msgbox(Microsoft.VisualBasic.Right(str,5))

Msgbox(Microsoft.VisualBasic.Right(str,5))

'World"

'世界”

Same goes for Left() too.

Left() 也是如此。

回答by Md. Suman Kabir

The accepted answer of this post will cause error in the case when the string length is lest than 5. So i have a better solution. We can use this simple code :

在字符串长度小于 5 的情况下,本文接受的答案将导致错误。所以我有一个更好的解决方案。我们可以使用这个简单的代码:

If(str.Length <= 5, str, str.Substring(str.Length - 5))

You can test it with variable length string.

您可以使用可变长度字符串对其进行测试。

    Dim str, result As String
    str = "11!"
    result = If(str.Length <= 5, str, str.Substring(str.Length - 5))
    MessageBox.Show(result)
    str = "I will be going to school in 2011!"
    result = If(str.Length <= 5, str, str.Substring(str.Length - 5))
    MessageBox.Show(result)

Another simple but efficient solution i found :

我发现的另一个简单但有效的解决方案:

str.Substring(str.Length - Math.Min(5, str.Length))

str.Substring(str.Length - Math.Min(5, str.Length))

回答by technoman23

I opened this thread looking for a quick solution to a simple question, but I found that the answers here were either not helpful or overly complicated. The best way to get the last 5 chars of a string is, in fact, to use the Right() method. Here is a simple example:

我打开这个线程寻找一个简单问题的快速解决方案,但我发现这里的答案要么没有帮助,要么过于复杂。实际上,获取字符串最后 5 个字符的最佳方法是使用 Right() 方法。这是一个简单的例子:

Dim sMyString, sLast5 As String

sMyString = "I will be going to school in 2011!"
sLast5 = Right(sMyString, - 5)
MsgBox("sLast5 = " & sLast5)

If you're getting an error then there is probably something wrong with your syntax. Also, with the Right() method you don't need to worry much about going over or under the string length. In my example you could type in 10000 instead of 5 and it would just MsgBox the whole string, or if sMyString was NULL or "", the message box would just pop up with nothing.

如果您收到错误消息,那么您的语法可能有问题。此外,使用 Right() 方法,您无需担心超出或低于字符串长度。在我的示例中,您可以输入 10000 而不是 5,它只会 MsgBox 整个字符串,或者如果 sMyString 为 NULL 或 "",则消息框将什么也没有弹出。

回答by dinesh

Dim a As String = Microsoft.VisualBasic.right("I will be going to school in 2011!", 5)
MsgBox("the value is:" & a)

回答by Toni

Old thread, but just only to say: to use the classic Left(), Right(), Mid()right now you don't need to write the full path (Microsoft.VisualBasic.Strings). You can use fast and easily like this:

旧线程,但只是说:要使用经典的Left(), Right()Mid()现在您无需编写完整路径 ( Microsoft.VisualBasic.Strings)。您可以像这样快速轻松地使用:

Strings.Right(yourString, 5)