vba 在visual basic中从指定索引处的字符串中获取字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17127272/
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-08 15:53:03  来源:igfitidea点击:

Getting char from string at specified index in the visual basic

vbaword-vba

提问by Yoda

As stated how to get char from string at specified index in the visual basic? I look through google and these do not work:

如前所述,如何在visual basic中从指定索引处的字符串中获取字符?我浏览了谷歌,这些都不起作用:

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

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

So how to get char at specified index?

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

回答by Kazimierz Jawor

If sis your string than you could do it this way:

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

Mid(s, index, 1)

Editbased on comment below question.

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

It seems that you need a bit different approach which should be easier. Try in this way:

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

Dim character As String 'Integer if for numbers
's = ActiveDocument.Content.Text - we don't need it
character = Activedocument.Characters(index)

回答by Adam111p

Getting one char from string at specified index

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

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

outStr="b"

outStr="b"

回答by Bhanu Sinha

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