Как из строки сделать массив vba
Перейти к содержимому

Как из строки сделать массив vba

How to: Convert a String to an Array of Characters in Visual Basic

Sometimes it is useful to have data about the characters in your string and the positions of those characters within your string, such as when you are parsing a string. This example shows how you can get an array of the characters in a string by calling the string’s ToCharArray method.

Example 1

This example demonstrates how to split a string into a Char array, and how to split a string into a String array of its Unicode text characters. The reason for this distinction is that Unicode text characters can be composed of two or more Char characters (such as a surrogate pair or a combining character sequence). For more information, see TextElementEnumerator and The Unicode Standard.

Example 2

It is more difficult to split a string into its Unicode text characters, but this is necessary if you need information about the visual representation of a string. This example uses the SubstringByTextElements method to get information about the Unicode text characters that make up a string.

Практическое руководство. Преобразование строки (String) в массив символов в Visual Basic

Иногда бывает полезно иметь данные о символах в строке и позициях этих символов в строке, например при анализе строки. В этом примере показано, как можно получить массив символов в строке, вызвав метод строки ToCharArray .

Пример 1

В этом примере показано, как разделить строку на Char массив и как разбить строку на String массив символов Юникода. Это различие состоит в том, что символы текста в Юникоде могут состоять из двух или более Char символов (например, суррогатной пары или последовательности присоединяемых символов). Дополнительные сведения см. в разделе TextElementEnumerator и TextElementEnumerator.

Пример 2

Сложнее разбить строку на символы в Юникоде, но это необходимо, если требуются сведения о визуальном представлении строки. В этом примере метод используется SubstringByTextElements для получения сведений о символах текста Юникода, составляющих строку.

Split string into array of characters?

How is it possible to split a VBA string into an array of characters?

I tried Split(my_string, «») but this didn’t work.

7 Answers 7

Safest & simplest is to just loop;

If your guaranteed to use ansi characters only you can;

You can just assign the string to a byte array (the reverse is also possible). The result is 2 numbers for each character, so Xmas converts to a byte array containing <88,0,109,0,97,0,115,0>
or you can use StrConv

which will give you <88,109,97,115>but in that case you cannot assign the byte array back to a string.
You can convert the numbers in the byte array back to characters using the Chr() function

Here’s another way to do it in VBA.

the problem is that there is no built in method (or at least none of us could find one) to do this in vb. However, there is one to split a string on the spaces, so I just rebuild the string and added in spaces.

To split a string into an array of sub-strings of any desired length:

You can pass the string into it either as a string:

…or already declard as a variant:

If the length of the desired sub-string doesn’t divide equally into the length of the string, the uppermost element of the array will be shorter. (It’ll be equal to strLength Mod subStrLength. Which is probably obvious.)

I found that most-often I use it to split a string into single characters, so I added another function, so I can be lazy and not have to pass two variables in that case:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *