English 中文(简体)
2. 在扼杀中达到最后5个特性
原标题:Get last 5 characters in a string
  • 时间:2010-05-11 18:44:32
  •  标签:
  • vb.net

我想从地狱中找到最后5位数/位数。 例如,<代码> 我将到2011年<<>>>>>>学校读到“2011年>>>>>。

任何想法? 我知道视觉基础有<条码> 权利(显示,5);这给我带来了错误。

最佳回答
str.Substring(str.Length - 5)
问题回答

校外检查:

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

错误核对:

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

旧读,但只是说:使用经典的<代码>,Right(,Mid(>),现在你不需要写全条道路()。 微软.VisualBasic.Strings。 你们可以快速、轻易地利用:

Strings.Right(yourString, 5)

在VB 2008年(VB 9.0)和后来作为微软的预先设定权利。 视觉基础。 权利(特征、数量)

Dim str as String = “Hello World”

Msg Box (Microsoft.VisualBasic.Right(str,5)

世界”

Same也站在左边。

该员额的公认答案将造成5年以下的错误。 因此,我有更好的解决办法。 我们可以利用这一简单法典:

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

你们可以用不同的长度加以测试。

    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)

另一种简单但有效的解决办法是:

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

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




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签