Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting char from string at specified index

Tags:

ms-word

vba

As stated how to get char from string at specified index in VBA (Visual Basic for Applications)? I searched Google and these do not work:

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

So how to get char at specified index?

like image 512
Yoda Avatar asked Jun 15 '13 19:06

Yoda


People also ask

How do I get the character at the specific index of a string?

You can get the character at a particular index within a string by invoking the charAt() accessor method. The index of the first character is 0, while the index of the last character is length()-1 . For example, the following code gets the character at index 9 in a string: String anotherPalindrome = "Niagara.

Can you access a string by index?

Strings are ordered sequences of character data, 00:15 and the individual characters of a string can be accessed directly using that numerical index. String indexing in Python is zero-based, so the very first character in the string would have an index of 0 , 00:30 and the next would be 1 , and so on.

Which method returns the character at specified index?

The charAt() method returns the character at a specified index (position) in a string.


2 Answers

If s is your string than you could do it this way:

Mid(s, index, 1)

Edit based 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)
like image 174
Kazimierz Jawor Avatar answered Oct 06 '22 07:10

Kazimierz Jawor


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"

like image 36
Adam111p Avatar answered Sep 27 '22 21:09

Adam111p