如何在字符串中搜索字符"
。搜索字符串中的字符
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