C++ 返回char数组中某个字符的下标

银霜覆秋枫 2015-05-25 10:56:50
C#中的Encoding.ASCII.GetString(byte[])可以把一个byte型的数组转化为string,然后string的string.indexof()可以返回指定字符在string中的下标。
然后我的问题是,我使用socket的recv函数得到了一个char型的ASCII码数组,C++中string的构造函数可以将由char数组直接生成一个string对象,但是C++中的string 类没有C#中string.indexof()函数,那我要返回string中的某个ASCII字符的下标如何处理?

或者可以直接返回指定ASCII字符在char数组中的下标也行,

除了遍历。。。。
...全文
721 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
银霜覆秋枫 2015-05-27
  • 打赏
  • 举报
回复
引用 6 楼 OExpress 的回复:
什么方法,拿出类分享下,帮顶 呵呵
string类有find函数。find_first_of,find_last_of等函数可以用~
银霜覆秋枫 2015-05-27
  • 打赏
  • 举报
回复
引用 7 楼 paschen 的回复:
CString 对象用Find, i = str.Find("a"); 如果找到,返回值是字符的位置
可以str.find,也有find_first_of之类的东西,和C#有些区别,最开始没找到~
银霜覆秋枫 2015-05-27
  • 打赏
  • 举报
回复
引用 8 楼 yangyunzhao 的回复:
引用 3 楼 u011974126 的回复:
[quote=引用 2 楼 FightForProgrammer 的回复:] 直接用下标操作符【】就行了啊
下标是我需要寻找的啊~我需要在 接收到的char型的数组中找到 某一个指定字符的下标,这样才方便我对这个数组进行划分提取。。。
.find[/quote]对的哈 ~
yangyunzhao 2015-05-26
  • 打赏
  • 举报
回复
引用 3 楼 u011974126 的回复:
引用 2 楼 FightForProgrammer 的回复:
直接用下标操作符【】就行了啊
下标是我需要寻找的啊~我需要在 接收到的char型的数组中找到 某一个指定字符的下标,这样才方便我对这个数组进行划分提取。。。
.find
paschen 版主 2015-05-26
  • 打赏
  • 举报
回复
CString 对象用Find, i = str.Find("a"); 如果找到,返回值是字符的位置
CyberLogix 2015-05-26
  • 打赏
  • 举报
回复
什么方法,拿出类分享下,帮顶 呵呵
银霜覆秋枫 2015-05-26
  • 打赏
  • 举报
回复
我已经找到解决办法了~来人送分啦~
银霜覆秋枫 2015-05-26
  • 打赏
  • 举报
回复
引用 2 楼 FightForProgrammer 的回复:
直接用下标操作符【】就行了啊
下标是我需要寻找的啊~我需要在 接收到的char型的数组中找到 某一个指定字符的下标,这样才方便我对这个数组进行划分提取。。。
FightForProgrammer 2015-05-26
  • 打赏
  • 举报
回复
直接用下标操作符【】就行了啊
银霜覆秋枫 2015-05-26
  • 打赏
  • 举报
回复
高手指点下吧
赵4老师 2015-05-26
  • 打赏
  • 举报
回复
CString Class Members Construction The String as an Array Assignment/Concatenation Comparison Extraction Other Conversions Searching Archive/Dump Buffer Access Windows-Specific Construction CString Constructs CString objects in various ways. The String as an Array GetLength Returns the number of characters in a CString object. For multibyte characters, counts each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters. IsEmpty Tests whether a CString object contains no characters. Empty Forces a string to have 0 length. GetAt Returns the character at a given position. operator [] Returns the character at a given position — operator substitution for GetAt. SetAt Sets a character at a given position. operator LPCTSTR Directly accesses characters stored in a CString object as a C-style string. Assignment/Concatenation operator = Assigns a new value to a CString object. operator + Concatenates two strings and returns a new string. operator += Concatenates a new string to the end of an existing string. Comparison operator == <, etc. Comparison operators (case sensitive). Compare Compares two strings (case sensitive). CompareNoCase Compares two strings (case insensitive). Collate Compares two strings (case sensitive, uses locale-specific information). CollateNoCase Compares two strings (case insensitive, uses locale-specific information). Extraction Mid Extracts the middle part of a string (like the Basic MID$ function). Left Extracts the left part of a string (like the Basic LEFT$ function). Right Extracts the right part of a string (like the Basic RIGHT$ function). SpanIncluding Extracts a substring that contains only the characters in a set. SpanExcluding Extracts a substring that contains only the characters not in a set. Other Conversions MakeUpper Converts all the characters in this string to uppercase characters. MakeLower Converts all the characters in this string to lowercase characters. MakeReverse Reverses the characters in this string. Replace Replaces indicated characters with other characters. Remove Removes indicated characters from a string. Insert Inserts a single character or a substring at the given index within the string. Delete Deletes a character or characters from a string. Format Format the string as sprintf does. FormatV Formats the string as vsprintf does. TrimLeft Trim leading whitespace characters from the string. TrimRight Trim trailing whitespace characters from the string. FormatMessage Formats a message string. Searching Find Finds a character or substring inside a larger string. ReverseFind Finds a character inside a larger string; starts from the end. FindOneOf Finds the first matching character from a set. Archive/Dump operator << Inserts a CString object to an archive or dump context. operator >> Extracts a CString object from an archive. Buffer Access GetBuffer Returns a pointer to the characters in the CString. GetBufferSetLength Returns a pointer to the characters in the CString, truncating to the specified length. ReleaseBuffer Releases control of the buffer returned by GetBuffer. FreeExtra Removes any overhead of this string object by freeing any extra memory previously allocated to the string. LockBuffer Disables reference counting and protects the string in the buffer. UnlockBuffer Enables reference counting and releases the string in the buffer. Windows-Specific AllocSysString Allocates a BSTR from CString data. SetSysString Sets an existing BSTR object with data from a CString object. LoadString Loads an existing CString object from a Windows resource. AnsiToOem Makes an in-place conversion from the ANSI character set to the OEM character set. OemToAnsi Makes an in-place conversion from the OEM character set to the ANSI character set. CString Overview | Hierarchy Chart

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧