3

How do trim off characters in a string, by how much you want?

For example, say your string is "Tony", but you wanted to display "ny" by trimming of the first two characters, how can this be done?

Sub Main()

Dim s As String
Dim Result As String

s = "Tony"
Result = LTrim(s)

msgbox(Result)

I have this so far using the LTrim function, so how do you specify by how much you want to cut to just display "ny" in the MessageBox?

4

3 回答 3

1

You don't want LTrim. You want Right:

Result = Right(s, Len(s) - 2);

This will take all but the two left-most characters of s.

于 2013-08-03T02:47:45.493 回答
1

嗯...如果我想剪掉字符串的开头,我会使用两个函数:StrReverse 和 Remove。

我会先反转字符串,然后使用remove函数切断现在的结尾,然后再次使用reverse函数将剩余的字符串翻转回原来的状态。

代码看起来像这样:

    Dim s As String = "Anthony"
    Dim index As Integer = 2

    Debug.Print(StrReverse(StrReverse(s).Remove(2)))

它的输出将是“ny”,长度将对应于索引。

于 2013-08-06T17:41:44.753 回答
0

您可以使用附加的字符串函数来做同样的事情,例如:

X$ = RIGHT$(V$, 2) ' get the ending 2 chars of string
X$ = LEFT$(V$, 2) ' get the leading 2 chars of string
X$ = MID$(V$, 2, 2) ' get 2 chars from the inside of string
于 2016-07-27T01:05:23.610 回答