Skip to main content
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How to: Convert a String to an Array of Characters in Visual Basic
- Article
- 09/15/2021
- 2 minutes to read
In this article
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.
Dim testString1 As String = "ABC"
' Create an array containing "A", "B", and "C".
Dim charArray[] As Char = testString1.ToCharArray
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.
' This string is made up of a surrogate pair [high surrogate
' U+D800 and low surrogate U+DC00] and a combining character
' sequence [the letter "a" with the combining grave accent].
Dim testString2 As String = ChrW[&HD800] & ChrW[&HDC00] & "a" & ChrW[&H300]
' Create and initialize a StringInfo object for the string.
Dim si As New System.Globalization.StringInfo[testString2]
' Create and populate the array.
Dim unicodeTestArray[si.LengthInTextElements - 1] As String
For i As Integer = 0 To si.LengthInTextElements - 1
unicodeTestArray[i] = si.SubstringByTextElements[i, 1]
Next
See also
- Chars[]
- System.Globalization.StringInfo
- How to: Access Characters in Strings
- Converting Between Strings and Other Data Types in Visual Basic
- Strings
Feedback
Submit and view feedback for
In this article, we’ll look at how to convert a string to an array of characters in Java. I'll also briefly explain to you what strings, characters, and arrays are. Characters are primitive datatypes. A character is a single character enclosed inside single quotation marks. It can be a letter, a digit, a punctuation mark, a space or something similar. For example: Strings are objects [reference type]. A string is made up of a string of characters. It's anything inside double quotation marks. For example: Arrays are fundamental data structures which can store fixed number of elements of the same data type in Java. For example, let's declare an array of characters: Now, we have a basic understanding about what strings, characters, and arrays are. What is a Character in Java?
char firstVowel = 'a';
What is a String in Java?
String vowels = "aeiou";
What is an Array in Java?
char[] vowelArray = {'a', 'e', 'i', 'o', 'u'};
Let's Convert a String to a Character Array
1. Use toCharArray[]
Instance Method
toCharArray[]
is an instance method of the String
class. It returns a new character array based on the current string object.
Let's check out an example:
// define a string
String vowels = "aeiou";
// create an array of characters
char[] vowelArray = vowels.toCharArray[];
// print vowelArray
System.out.println[Arrays.toString[vowelArray]];
Output: [a, e, i, o, u]
When we convert a string to an array of characters, the length remains the same. Let's check the length of both vowels
and vowelArray
:
System.out.println["Length of \'vowels\' is " + vowels.length[]];
System.out.println["Length of \'vowelArray\' is " + vowelArray.length];
Output:
Length of 'vowels' is 5
Length of 'vowelArray' is 5
We can use various ways to print an array. I used the toString[]
static method from the Arrays
utility class.
You can read more about the toCharArray[]
instance method in the Java documentation.
2. Use charAt[] Instance Method
charAt[]
is an instance method of the String
class. It returns a character at the specified index of the current string.
NOTE: a string is zero index based, similar to an array.
Let's see how we
can convert a string to an array of characters using charAt[]
:
// define a string
String vowels = "aeiou";
// create an array of characters. Length is vowels' length
char[] vowelArray = new char[vowels.length[]];
// loop to iterate each characters in the 'vowels' string
for [int i = 0; i < vowels.length[]; i++] {
// add each character to the character array
vowelArray[i] = vowels.charAt[i];
}
// print the array
System.out.println[Arrays.toString[vowelArray]];
Output: [a, e, i, o, u]
You can read more about the charAt[]
instance method in the Java documentation.
I just showed you another way of converting a string to a character array, but we can use the toCharArray[]
method to easily convert instead of creating loops and iterating them.
Please feel free to let me know if you have any suggestions or questions.
Photo by Alex Alvarez on Unsplash.
Please support freeCodeCamp in their Data Science Curriculum Pledge Drive.
Connect with me on Medium.
Thank you 😇
Happy Coding ❤️
More on Programming in Java
- Object-Oriented Programming Principles in Java: OOP Concepts for Beginners
- Java Array Methods – How to Print an Array in Java
- Java String to Int – How to Convert a String to an Integer
- Java Random Number Generator – How to Generate Integers With Math Random
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started