有一个字符串如何从第一个' /r/n/r/n '得到它的内容,直到2行从字符串结束
Having a string how to get its contents from first `/r/n/r/n` until 2 lines from string end?
我尝试在c++中创建简单的文件上传服务。我将所有用户请求体作为一个大字符串。用户可以上传任何类型的数据。我只需要从请求正文字符串中获取用户文件内容。
因此,例如,现在我有下一个代码与我的服务API提供商工作:
std::cout << "Request body: " << request->body << std::endl << "Request size: " << request->body.length() << std::endl;
,这将打印为:
Request body: ------WebKitFormBoundaryAZlJcLinxYi6OCzX
Content-Disposition: form-data; name="datafile"; filename="crossdomain.xml"
Content-Type: text/xml
я╗┐<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-
omain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" to-ports="*" />
</cross-domain-policy>
------WebKitFormBoundaryAZlJcLinxYi6OCzX--
Request size: 411
所以我需要从请求->体(这是字符串)从第一个/r/n/r/n
直到最后一行-2行的所有数据。如何做这样的事情与字符串在c++ ?
这不是最优雅的方法,但是一种选择是这样做:
std::string contents = /* ... get the string ... */
/* Locate the start point. */
unsigned startPoint = contents.find("rnrn");
if (startPoint == string::npos) throw runtime_error("Malformed string.");
/* Locate the end point by finding the last newline, then backing up
* to the newline before that.
*/
unsigned endPoint = contents.rfind('n');
if (endPoint == string::npos || endPoint == 0) throw runtime_error("Malformed string.");
endPoint = contents.rfind('n', endPoint - 1);
if (endPoint == string::npos) throw runtime_error("Malformed string.");
/* Hand back that slice of the string. */
return std::string(contents.begin() + startPoint, contents.begin() + endPoint);
您可以使用正则表达式。这个页面有一些不错的c++示例:http://www.math.utah.edu/docs/info/libg++_19.html
相关文章:
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 读取字符串直到标志 (C++)
- 如何输入每个单词在字符串数组中的句子,直到用户按Enter键
- C++从文件中读取不同类型的数据,直到有一个以数字开头的字符串
- 读取字符串,直到无getline中的C 中的新行
- 读取 iostream,直到找到字符串分隔符
- 从矢量读取字符串,直到C 的空格
- CAtlRegExp:匹配一个字符串直到十六进制集
- 一次读取一个字符的字符串,直到行或文件末尾
- 从输入文件中获取字符串直到整数?C++
- 从字符串中删除字符,直到它变为空
- 读取字符串直到行尾
- 如何从一个文件中读取字符串、char、int,直到在c++中找到eof
- 复制字符串直到'.' 以及当我知道结构时如何仅复制数字
- 如何从stdin读取字符串,直到遇到空行
- c++将文本附加到字符串中,直到看到特定的字符
- 读取直到boost::asio::streambuf中的字符串分隔符
- 按字符读取字符串,直到行尾
- 有一个字符串如何从第一个' /r/n/r/n '得到它的内容,直到2行从字符串结束
- 运行文件直到*字符串*,然后读取列直到*没有十进制*