CString 解析回车

发布于 2024-11-29 16:57:02 字数 200 浏览 0 评论 0原文

假设我有一个包含多个回车符的字符串,即:

394968686
100630382
395950966
335666021

我对 C++ 还很业余,有人愿意告诉我如何进行操作:解析字符串中的每一“行”吗?所以我可以稍后用它做一些事情(将所需的行添加到列表中)。我猜在循环中使用 Find("\n") ?

谢谢你们。

Let's say I have a string that has multiple carriage returns in it, i.e:

394968686
100630382
395950966
335666021

I'm still pretty amateur hour with C++, would anyone be willing to show me how you go about: parsing through each "line" in the string ? So I can do something with it later (add the desired line to a list). I'm guessing using Find("\n") in a loop?

Thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

も让我眼熟你 2024-12-06 16:57:02
while (!str.IsEmpty())
{
    CString one_line = str.SpanExcluding(_T("\r\n"));
    // do something with one_line
    str = str.Right(str.GetLength() - one_line.GetLength()).TrimLeft(_T("\r\n"));
}

此代码将消除空行,但如果需要,很容易纠正。

while (!str.IsEmpty())
{
    CString one_line = str.SpanExcluding(_T("\r\n"));
    // do something with one_line
    str = str.Right(str.GetLength() - one_line.GetLength()).TrimLeft(_T("\r\n"));
}

Blank lines will be eliminated with this code, but that's easily corrected if necessary.

菊凝晚露 2024-12-06 16:57:02

您可以尝试使用 stringstream。请注意,您可以重载 getline 方法以使用您想要的任何分隔符。

string line;
stringstream ss;
ss << yourstring;
while ( getline(ss, line, '\n') )
{
  cout << line << endl;
}

或者,您可以使用 boost 库的 tokenizer 类。

You could try it using stringstream. Notice that you can overload the getline method to use any delimeter you want.

string line;
stringstream ss;
ss << yourstring;
while ( getline(ss, line, '\n') )
{
  cout << line << endl;
}

Alternatively you could use the boost library's tokenizer class.

寻梦旅人 2024-12-06 16:57:02

您可以在 C++ 中使用 stringstream 类。

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
   string str = "\
                394968686\
                100630382\
                395950966\
                335666021";
   stringstream ss(str);
   vector<string> v;

   string token;
   // get line by line
   while (ss >> token)
   {
      // insert current line into a std::vector
      v.push_back(token);
      // print out current line
      cout << token << endl;
   }
}

Output of the program above:

394968686
100630382
395950966
335666021

请注意,使用运算符>>时,解析的标记中不会包含空格。请参阅下面的评论。

You can use stringstream class in C++.

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
   string str = "\
                394968686\
                100630382\
                395950966\
                335666021";
   stringstream ss(str);
   vector<string> v;

   string token;
   // get line by line
   while (ss >> token)
   {
      // insert current line into a std::vector
      v.push_back(token);
      // print out current line
      cout << token << endl;
   }
}

Output of the program above:

394968686
100630382
395950966
335666021

Note that no whitespace will be included in the parsed token, with the use of operator>>. Please refer to comments below.

月野兔 2024-12-06 16:57:02

如果您的字符串存储在 c 风格的 char* 或 std::string 中,那么您只需搜索 \n 即可。

std::string s;
size_t pos = s.find('\n');

您可以使用 string::substr() 来获取子字符串并将其存储在列表中。伪代码,

std::string s = " .... ";
for(size_t pos, begin = 0; 
    string::npos != (pos = s.find('\n'));
    begin = ++ pos)
{
  list.push_back(s.substr(begin, pos));
}

If your string is stored in a c-style char* or std::string then you can simply search for \n.

std::string s;
size_t pos = s.find('\n');

You can use string::substr() to get the substring and store it in a list. Pseudo code,

std::string s = " .... ";
for(size_t pos, begin = 0; 
    string::npos != (pos = s.find('\n'));
    begin = ++ pos)
{
  list.push_back(s.substr(begin, pos));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文