一个字符串转换为字符数组

一个字符串转换为字符数组

本文介绍了一个字符串转换为字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有这样的字符串。

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?

推荐答案

每一个字符串是一个隐含的字符数组。所以,你可以取得第三个字符:

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

Dim char3 = str(2)

修改:只是为了完整性的缘故。您也可以使用字符串实例转换到一个新的字符数组实例。采用的核心利益 ToCharArray 是您收到的字符数组是可变的,这意味着你可以真正改变每个个性。

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.

请注意,您也可以使用 LINQ 。比如你想要一个字符串的前三个字母:

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()

这篇关于一个字符串转换为字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 02:26