21

如前所述,如何从 VBA(Visual Basic for Applications)中指定索引处的字符串中获取字符?我搜索了谷歌,这些不起作用:

s(index), s.Chars(index),s,Characters(index)

那么如何在指定索引处获取字符呢?

4

3 回答 3

38

如果s是你的字符串,那么你可以这样做:

Mid(s, index, 1)

根据以下问题的评论进行编辑。

似乎您需要一些不同的方法,这应该更容易。以这种方式尝试:

Dim character As String 'Integer if for numbers
's = ActiveDocument.Content.Text - we don't need it
character = Activedocument.Characters(index)
于 2013-06-15T19:49:46.227 回答
1

从指定索引处的字符串中获取一个字符

Dim pos As Integer
Dim outStr As String
pos = 2 
Dim outStr As String
outStr = Left(Mid("abcdef", pos), 1)

outStr="b"

于 2015-12-18T16:11:19.850 回答
0
char = split_string_to_char(text)(index)

------

Function split_string_to_char(text) As String()

   Dim chars() As String
   For char_count = 1 To Len(text)
      ReDim Preserve chars(char_count - 1)
      chars(char_count - 1) = Mid(text, char_count, 1)
   Next
   split_string_to_char = chars
End Function
于 2019-08-30T04:52:08.463 回答