如何在字符串中搜索字符"
。搜索字符串中的字符
0
A
回答
2
您可以在未排序的字符串查找的字符做的最好的是线性搜索:
for each index in the string:
if the character at that index matches the search criteria:
report the current index
report not found
有,当然,做这件事已经是一个功能:std::string::find,这将返回std::string::npos如果字符未找到;否则,该函数将返回首次找到该字符的索引。还有像std::string::find_first_of,std::string::find_last_of这样的查找变体,以及它们的“not_of”变体。
2
3
您可以使用string :: find()函数。参观here更多信息
#include <string>
using namespace std;
int main()
{
string str ("foo\"bar");
string str2 ("\"");
size_t found;
//you may want to do str.find('"') if you are only looking for one char.
found=str.find(str2);
}
它逃跑定义字符串里面的“字符是非常重要的
+0
或者不用分配一个新的字符串来保存''\”“'''''''''''' – 2010-05-09 13:28:07
+0
@wintermute谢谢你 – msemelman 2010-05-09 18:01:32
2
下面是简单的解决方案:
#include <string.h>
using namespace std;
int findChar(char c, string myString)
{
pos = myString.find(c); // Find position in the string (to access myString[pos])
return int(pos);
}
0
这是没有解决任何依赖与C++中的库
char * foo = "abcdefg";
char cf = 'e'; // Char to find
int f = 0; while (*(foo + f++) != cf); // f is 5
相关问题
- 1. 搜索字符串中的字符串
- 2. 搜索字符串内的字符串
- 3. VBA搜索字符串,字符串
- 4. 搜索字符串
- 5. 字符串搜索
- 6. 搜索字符串
- 7. 搜索字符串
- 8. 字符串搜索
- 9. 搜索字符串
- 10. 搜索字符串
- 11. sed命令来搜索字符串和搜索的字符串
- 12. 搜索特定字符的字符串
- 13. 某个字符的搜索字符串
- 14. 搜索字符串文字
- 15. hw搜索索引字符串列表中的子字符串?
- 16. 在字符串中搜索'$'
- 17. 搜索字符串中的符号
- 18. 字符串中的PHP搜索字
- 19. 字符串中的oracle搜索字
- 20. Python - 搜索字典中的字符串
- 21. 字符串切片,搜索字符
- 22. Python的搜索字符串
- 23. Java的搜索字符串
- 24. 在C++中搜索字符串中的字符串
- 25. jQuery在comb中的字符串中搜索字符串。用AJAX
- 26. 搜索并删除字符串中的子字符串
- 27. PHP:搜索字符串中的字符串
- 28. 搜索字符串数组中的字符串
- 29. AngularJS md-autocomplete搜索字符串中的字符串
- 30. 在字符串数组中搜索精确的字符串?
你期待在那个角色中找到什么?还总是记得第四次修正案。 :-) – Thomas 2010-05-09 07:23:17
你的意思是'\“'字符吗? – qba 2010-05-09 18:11:42