zoukankan      html  css  js  c++  java
  • string替换字符串,路径的斜杠替换为下划线

    场景

    替换某个路径的所有“”为“_”。

    很多时候取证需要把恶意代码文件取回来,然后清除。

    如果在D:WEB模板制作插件需要覆盖CodeColoringCodeColoring.xml这样的目录下,我会把路径也一并记录下来。

    D_WEB_模板制作插件_需要覆盖_CodeColoring_CodeColoring.xml
    

    技术思路

    分别替换掉“:"和“”就可以了

    string::npos

    npos 是一个常数,用来表示不存在的位置

    find

    find函数的返回值是整数,假如字符串存在包含关系,其返回值必定不等于npos,但如果字符串不存在包含关系,那么返回值就一定是npos。

    replace

    replace(const size_type _Off, // 索引值
    const size_type _N0,          // 替换位数
    const basic_string& _Right    // 替换的字符串
    )
    

    封装替换函数的代码

    #include   <string>     
    #include   <iostream>     
    using   namespace   std;     
    string& replace_all(string& str,const string& old_value,const string& new_value)     
    {     
    	while (true) 
    	{
    		string::size_type   pos(0);
    		if ((pos = str.find(old_value)) != string::npos)
    		{
    			str.replace(pos, old_value.length(), new_value);
    		}
    		else
    		{
    			break;
    		}
    	}  
        return   str;     
    }     
    string& replace_all_distinct(string& str,const string& old_value,const string&   new_value)     
    {     
        for(string::size_type   pos(0);   pos!=string::npos;   pos+=new_value.length())   {     
            if((pos=str.find(old_value,pos))!=string::npos)     
            {
                str.replace(pos,old_value.length(),new_value);   
            }
            else   
            {
                break;
             }
        }     
        return   str;     
    }     
    
    int   main()     
    {     
        cout   <<   replace_all(string("12212"),"12","21")   <<   endl;     
        cout   <<   replace_all_distinct(string("12212"),"12","21")   <<   endl;     
    }   
    
      
    /* 
    输出如下:    
    22211    
    21221 
    */  
    

    代码

    - 声明部分
    
    string & replace_all(string& str, const string& old_value, const string& new_value);
    		
    - 使用部分
    
    // 替换所有的“\”变成“_”
    string new_file_name = replace_all(new_path_name, "\", "_");
    // 替换所有的“:”变成“_”
    new_file_name = replace_all(new_path_name, ":", "_");
    	
    - 定义部分
    
    string & CMFCClistDlg::replace_all(string & str,const string & old_value,const string & new_value)
    {
    	// TODO: 在此处插入 return 语句
    	while (true) 
    	{
    		string::size_type   pos(0);
    		if ((pos = str.find(old_value)) != string::npos)
    		{
    			str.replace(pos, old_value.length(), new_value);
    		}
    		else
    		{
    			break;
    		}
    	}
    	return   str;
    }
    

    参考

    string替换所有指定字符串(C++)

    https://www.cnblogs.com/catgatp/p/6407783.html

    c++中string 的replace用法

    https://blog.csdn.net/yinhe888675/article/details/50920836

    C++: string 中find函数的用法以及string::npos的含义

    https://blog.csdn.net/linwh8/article/details/50752733

  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/17bdw/p/10279565.html
Copyright © 2011-2022 走看看