荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: sephiroth (Dreams Come True), 信区: Program
标  题: C++技巧:使用trim_spaces函数的好处[转载]
发信站: 荔园晨风BBS站 (Fri Jan  3 21:44:24 2003), 站内信件

当处理字符串时,开发人员通常需要去掉前面或者后尾的空格。在这种情况下,使
用trim_spaces函数就会变得非常的方便。

这一函数特别有用,尤其在下面的几种情况下:

当剖析XML的时候(XML标签外的信息将被忽略)

当剖析HTML的时候(对于每一行,HTML中前面和末尾的空格都被忽略)

当从一个文件中读出文本的时候(通常情况下,前面和末尾的空格都被忽略)


函数trim_spaces具有几下的优点:

可以运用于任一字符类型(字符型,无符号字符型,wchar_t类型等)

能够运用于指向字符的指针(常量字符*,常量wchar_t等)以及STL(标准模板库)字
符串。

可以定义哪些字符被当成空格操作(通过传递两个参数给函数)或使用缺省设置。


缺省的字符设置被认为是' ' (空格), '\t' (标签), '\n' (换行), '\r' (回车),
 and '\f' (换页)。你也可以把不同的字符当成空格,这取决于你的需要。例如,
当你想从一个字符串中去掉回车号,你可以生成下面的函数:

  template< class CharType>
  std::basic_string< CharType>
  trim_enters( const std::basic_string< CharType> & strSource)
  {
  // enters
  static const CharType strDefaults[] = { '\n', '\r', 0};
  static std::basic_string< CharType> strEnters( strDefaults);
  return trim_spaces( strSource, strEnters);
  }
  template< class CharType>
  std::basic_string< CharType>
  trim_enters( const CharType * strSource)
  {
  return trim_enters( std::basic_string< CharType>( strSource));
  }
下面是trim_spaces 函数使用的代码:

  #include <string>
  #include <algorithm>
  #include <functional>
  template< class CharType>
  struct is_char_in_str
  : public std::binary_function< CharType, std::basic_string< CharType>,

  bool>
  {
  bool operator()( CharType ch, const std::basic_string< CharType> &
  strSource) const
  {
  int nFoundPos = strSource.find( ch);
  bool bInStr = (nFoundPos != std::basic_string< CharType>::npos);
  return bInStr;
  }
  };
  template< class CharType>
  std::basic_string< CharType> trim_spaces(
  const std::basic_string< CharType> & strSource,
  const std::basic_string< CharType> & strSpaces)
  {
  typedef std::basic_string< CharType> string_type;
  string_type::const_iterator
  itFirst = std::find_if(
  strSource.begin(), strSource.end(),
  std::not1(
  std::bind2nd( is_char_in_str< CharType>(), strSpaces)));
  string_type::const_reverse_iterator
  ritLast = std::find_if(
  strSource.rbegin(), strSource.rend(),
  std::not1(
  std::bind2nd( is_char_in_str< CharType>(), strSpaces)));
  string_type::const_iterator itLast = &*ritLast;
  if ( itFirst <= itLast)
  {
  if ( itFirst != strSource.end())
  {
  return string_type( itFirst, itLast + 1);
  }
  }
  // this string contains only spaces or is empty
  return string_type();
  }
  template< class CharType>
  std::basic_string< CharType>
  trim_spaces( const std::basic_string< CharType> & strSource)
  {
  // default spaces
  static const CharType strDefaults[] = { ' ', '\t', '\n', '\r', '\f',
0};
  static std::basic_string< CharType> strSpaces( strDefaults);
  return trim_spaces( strSource, strSpaces);
  }
  template< class CharType>
  std::basic_string< CharType>
  trim_spaces( const CharType * strSource)
  {
  return trim_spaces( std::basic_string< CharType>( strSource));
  }
  template< class CharType>
  std::basic_string< CharType>
  trim_spaces( const CharType * strSource, const CharType * strSpaces)

  {
  return trim_spaces(
  std::basic_string< CharType>( strSource),
  std::basic_string< CharType>( strSpaces));
  }
一个范例:

  #include <assert.h>
  int main(int argc, char* argv[])
  {
  assert( trim_spaces( "\n\t Test\n \t\r") == "Test");
  assert( trim_spaces( L"\n\t Test\n \t\r") == L"Test");
  assert( trim_spaces( "\n \tTest\n \t\r", "\r\n ") == "\tTest\n
  \t");
  assert( trim_spaces( L"\n \tTest\n \t\r", L"\r\n ") == L"\tTest\n
  \t");
  assert( trim_spaces( "\n\t \n \t\r") == "");
  assert( trim_spaces( "\n\t \n A \t\r") == "A");
  assert( trim_spaces( "\n\t \n Ab \t\r") == "Ab");
  return 0;
  }
--
         那一年,我生命中吹过的风,在这里轻轻地转了一个弯... ...

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.44.81]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店