如何获得字符串的第n个字符? [英] How can I get the nth character of a string?
本文介绍了如何获得字符串的第n个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,
char* str = "HELLO"
如果我只想从中获取E
,我该怎么做?
If I wanted to get just the E
from that how would I do that?
推荐答案
char* str = "HELLO";
char c = str[1];
请记住,C中的数组和字符串从0开始而不是从1开始索引,因此"H"为str[0]
,"E"为str[1]
,第一个"L"为str[2]
,依此类推.
Keep in mind that arrays and strings in C begin indexing at 0 rather than 1, so "H" is str[0]
, "E" is str[1]
, the first "L" is str[2]
and so on.
这篇关于如何获得字符串的第n个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文