Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vb.net - Converting a string to a char array

Let's say I have a string like this.

Dim str As String = "code"

I need to break this string down to an array of characters like this,

{"c", "o", "d", "e"}

How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Every string is an implicit char-array. So you can get the 3rd char by:

Dim char3 = str(2)

Edit: Just for the sake of completeness. You can also use String.ToCharArray to convert the string instance to a new char-array instance. The core benefit of using ToCharArray is that the char-array you receive is mutable, meaning you can actually change each individual character.

Note that you could also use LINQ. If you for example want the first three characters of a String:

Dim firstThree As Char() = str.Take(3).ToArray()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...