使用String :: Find函数

编程入门 行业动态 更新时间:2024-10-20 13:39:24
本文介绍了使用String :: Find函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在字符串中查找字符串,并且它不返回任何内容.....

I''m trying to find a string within a string and its not returning anything.....

if (client.SendHttpRequest()) { wstring httpResponseHeader = client.GetHttpResponseHeader(); string str ("wstring httpResponseHeader"); string str2 ("Content="); size_t found; found=str.find(str2); printf("\n%s\n\n",found); //wstring httpResponse = client.GetHttpResponse(); wprintf(L"HTTP Response Header:\n%s\n\n", httpResponseHeader.c_str()); //wprintf(L"HTTP Response:\n%s\n\n", httpResponse.c_str()); }

推荐答案

您期望找到什么?您搜索的字符串是"wstring httpResponseHeader",并且您正在搜索不属于该字符串的"Content =.然后,将find成员函数的结果存储在变量found中.请注意,此值是位置值,在这种情况下将返回预定义的常量string::npos(顺便说一下,它不等于0). 但是,您没有从printf语句中看到任何有用结果的原因则完全不同:您提供了一个格式字符串,该字符串期望一个类型为string的变量(格式说明符为%s),但是您提供了一个size_t类型的值,当然不适合.不幸的是,printf不会(实际上也不能)进行任何类型检查,因此它将尝试将string::npos转换为char指针,这当然会导致意外情况.啊,类型不安全的C函数的奇迹... 如果需要某种类型安全的输出,请使用iostrem,如下所示: What do you expect to find? The string your searching in is "wstring httpResponseHeader" and you''re searching for "Content=" which is not part of that string. Then you store the result of the find member function in the variable found. Note that this value is a position value, and in this case will return the predefioned constant string::npos (which by the way is not equal to 0). The reason you don''t see any useful result from your printf statement however is entirely different: you provide a format string that expects a variable of type string (format specifier %s), but you provide a value of type size_t, which of course doesn''t fit. Unfortunately printf does not (and in fact can not) do any type check, so it tries to convert string::npos into a char pointer, which of course results in something unexpected. Ahhh, the wonders of type-unsafe C functions... If you want some type-safe output, use iostrem, like this: #include <iostream> //... std::cout << std::endl << "find result: " << found << std::endl;

注意:std::endl等效于换行符;与``\ n''的主要区别是保证可以在任何操作系统上运行,包括需要额外的``\ r''(回车)的那些操作系统. PS:请查看以下内容: www.cplusplus/reference/string/string/find/ [ ^ ]

Note: std::endl is the equivalent of a line break; the main difference to ''\n'' is that it is guaranteed to work on any operating system, inlcuding those that require an additional ''\r'' (carriage return). P.S.: check this out: www.cplusplus/reference/string/string/find/[^]

您应该处理另一个爱好或学习编程的基础知识.给您买一本好书,或查看WorldWideWeb中的大量示例.为什么要从http协议开始?没有人会帮助您解决您的愚蠢重复问题. 问候. You should deal with another hobby or learning the basics of programming. Buy you a good book or check out the tons of examples in the WorldWideWeb. Why you are starting with http protocol? Nobody will help you with your stupid repeating questions. Regards.

更多推荐

使用String :: Find函数

本文发布于:2023-10-30 04:19:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   String   Find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!