如何检查行中的第一个字符是否为 #(注释的开头) [英] How to check if the first char in the line is # (beginning of a comment)

查看:26
本文介绍了如何检查行中的第一个字符是否为 #(注释的开头)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直遵循这个约定:

I have been following this convention thus far:

std::string line;
while(std::getline(in,line)) 
{
    if(line.size() && line[0] =='#')
      continue;
    /* parse text*/
}

明显的缺点是注释可能不会从第一个字符开始,在前导空格的情况下.

The obvious drawback is that comment may not begin at the first character, in the case of leading whitespace.

处理这种事情的好方法是什么?

What is the good way to deal with this sort of a thing?

推荐答案

简单的增强:你可能想使用 line.find_first_not_of(" ") 来获取第一个非空格,然后检查是否那个 是一个#".这也将涵盖零长度的情况.像这样的片段:

Simple enhancement: you may want to use line.find_first_not_of(" ") to get the first non-whitespace and then check if that is a '#'. That would also cover to the zero length case. Something like this fragment:

  found= line.find_first_not_of(" \t");

  if( found != string::npos)
  {
    if( line[found] == '#')
      continue;
  }

更多信息

这篇关于如何检查行中的第一个字符是否为 #(注释的开头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆