简体   繁体   English

取一串字母并将其放入数组中

[英]Taking a string of letters and placing them into an array

I have a string, "ABCDEFG" and I want to make an array that would look like this: 我有一个字符串“ ABCDEFG”,我想创建一个看起来像这样的数组:

array[0] = "A"
array[1] = "B"
array[2] = "C"
etc.

Do I have tokenize the original string, or is there some type of built in way to do this? 我是否已对原始字符串进行标记化,或者是否有某种内置方式可以做到这一点?

Thanks! 谢谢!

If you want to make an array of strings, you can do it with a loop: 如果要创建字符串数组,可以循环执行:

const char *str = "ABCDEFG";
string letters[7];
for (int i = 0 ; i != strlen(str) ; i++) {
    // The constructor below cuts out a single letter from the literal:
    letters[i] = string(&str[i], &str[i+1]);
}

Here is a demo on ideone . 这是有关ideone演示

For std::string original string, use substr ( demo ): 对于std::string原始字符串,请使用substrdemo ):

for (int i = 0 ; i != str.size() ; i++) {
    letters[i] = str.substr(i, 1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM