zoukankan      html  css  js  c++  java
  • CStrFun

     1 #ifndef _STR_FUN_H_030802
     2 #define _STR_FUN_H_030802
     3 
     4 #include <string>
     5 #include <sstream>
     6 #include <iostream>
     7 #include <algorithm>
     8 
     9 using namespace std;
    10 
    11 class CStrFun
    12 {
    13 public:
    14     CStrFun();
    15     virtual ~CStrFun();
    16     //把类中的函数都定义成静态函数,这样相当于在编译时就分配了空间,这样不需要实体对象也可以调用泪中的函数了。
    17     static void Str2Lower(string &sSource, int nLen);
    18     static string itos(long long i)
    19     {
    20         stringstream s;
    21         s << i;
    22         return s.str();
    23     }
    24 
    25     /* Locate a substring ignoring case
    26      * The  function  returns a value equal or lager than zero,
    27          * or -1 if the substring is not found.
    28      */
    29 
    30     static string::size_type FindCase(string haystack, string needle);
    31     static string::size_type FindCaseFrom(string haystack, string needle, int From);
    32 
    33     static void ReplaceStr(string &str, string srstr, string dsstr);
    34     static void EraseStr(string &str, string substr);
    35 };
    36 
    37 #endif    // end _STR_FUN_H_030802
    38 
    39 /*
    40 ostringstream oss<<Dea<<'.'<<pre();
    41 
    42 oss.str();
    43 */
     1 #include "StrFun.h"
     2 
     3 CStrFun::CStrFun() {
     4 
     5 }
     6 
     7 CStrFun::~CStrFun() {
     8 
     9 }
    10 
    11 void CStrFun::Str2Lower(string& str, int nLen) //将大写字母改写成小写字母
    12         {
    13     char distance = 'A' - 'a';
    14 
    15     for (int i = 0; i < nLen; i++)
    16         if (str[i] >= 'A' && str[i] <= 'Z')
    17             str[i] -= distance;
    18 }
    19 
    20 bool _nocase_compare(char c1, char c2) //不区分大小写的比较
    21         {
    22     return toupper(c1) == toupper(c2);
    23 }
    24 
    25 string::size_type CStrFun::FindCase(string haystack, string needle) //在haystack中找第一个匹配needle的位置
    26         {
    27     if (haystack.empty())
    28         return string::npos;
    29     if (needle.empty())
    30         return string::npos;
    31 
    32     string::iterator pos;
    33     pos = search(haystack.begin(), haystack.end(), needle.begin(), needle.end(),
    34             _nocase_compare);
    35 
    36     if (pos == haystack.end()) {
    37         return string::npos;
    38     } else {
    39         return (pos - haystack.begin());
    40     }
    41 }
    42 
    43 string::size_type CStrFun::FindCaseFrom(string haystack, string needle,
    44         int nFrom) //在haystack中的第n个位置找第一个匹配needle的位置
    45         {
    46     //assert( haystack.empty() == false );
    47     //assert( needle.empty() == false );
    48     if (haystack.empty())
    49         return string::npos;
    50     if (needle.empty())
    51         return string::npos;
    52 
    53     string::iterator pos;
    54     pos = search(haystack.begin() + nFrom, haystack.end(), needle.begin(),
    55             needle.end(), _nocase_compare);
    56 
    57     if (pos == haystack.end()) {
    58         return string::npos;
    59     } else {
    60         return (pos - haystack.begin());
    61     }
    62 }
    63 
    64 void CStrFun::EraseStr(string &str, string substr) //在str中删除所有出现的substr
    65         {
    66     if (str.size() == 0 || substr.size() == 0)
    67         return;
    68 
    69     string::size_type idx = 0;
    70     string::size_type sub_length = substr.length();
    71     idx = str.find(substr, idx);
    72     while (idx != string::npos) {
    73         str.erase(idx, sub_length);
    74         idx = str.find(substr, idx);
    75     }
    76 }
    77 
    78 void CStrFun::ReplaceStr(string&str, string srstr, string dsstr) //将str中的字串替换成dsstr
    79         {
    80     if (str.size() == 0 || srstr.size() == 0)
    81         return;
    82 
    83     string::size_type idx = 0;
    84     string::size_type sub_length = srstr.length();
    85     idx = str.find(srstr, idx);
    86     while (idx != string::npos) {
    87         str.replace(idx, sub_length, dsstr);
    88 
    89         if (idx + dsstr.size() > str.size())
    90             break;
    91 
    92         idx = str.find(srstr, idx + dsstr.size());
    93     }
    94 }
  • 相关阅读:
    ABP dynamic API
    SQL语句 还原未知逻辑名称数据库
    SQL 还原数据库
    使用开源项目集锦
    webp性能测评
    js通过继承实现私有函数
    js组合继承和寄生组合式继承比较
    JS中循环绑定遇到的问题及解决方法
    JS学习之事件流
    OpenGL利用模板测试实现不规则裁剪
  • 原文地址:https://www.cnblogs.com/kakamilan/p/2571973.html
Copyright © 2011-2022 走看看