C++ 从字符串的某些点删除空格

C++ 从字符串的某些点删除空格,c++,string,C++,String,所以我的问题是我必须从字符串中删除空格,但只能在字符串的某些位置删除。我有一个程序可以逐行读取.txt文件,文件中的行可以是以下类型: |sometext:anothertext| |some text : anothertext| |some text:another text | | sometext : anothertext | “|”正在标记线的起点和终点 现在我只需要删除行前面和后面以及“:”旁边的空格 编辑:似乎正则表达式是实现这一点的最简单方法我有一个

所以我的问题是我必须从字符串中删除空格,但只能在字符串的某些位置删除。我有一个程序可以逐行读取.txt文件,文件中的行可以是以下类型:

|sometext:anothertext|

|some text   :  anothertext|

|some text:another text |

| sometext    :    anothertext  |
“|”正在标记线的起点和终点

现在我只需要删除行前面和后面以及“:”旁边的空格


编辑:似乎正则表达式是实现这一点的最简单方法

我有一个实用程序类,它有一系列处理字符串的静态方法;我将只展示用于修剪空白的相关函数,以及分割字符串或使用分隔子字符串或字符标记字符串的方法

实用程序.h

#ifndef UTILITY_H
#define UTILITY_H

class Utility {
public:
    static std::string  trim( const std::string& str, const std::string elementsToTrim = " \t\n\r" );
    static std::vector<std::string> splitString( const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty = true ); 

private:
    Utility(); // Private - Not A Class Object
    Utility( const Utility& c ); // Not Implemented
    Utility& operator=( const Utility& c ); // Not Implemented

}; // Utility

#endif // UTILITY_H
\ifndef实用程序
#定义效用
类效用{
公众:
静态std::string trim(const std::string&str,const std::string elementsToTrim=“\t\n\r”);
静态std::vector splitString(const std::string&strStringToSplit,const std::string&strDelimiter,const bool keepmpty=true);
私人:
Utility();//Private-不是类对象
实用工具(const实用工具&c);//未实现
实用工具和运算符=(const实用工具和c);//未实现
}; // 效用
#endif//UTILITY\u H
实用程序.cpp

#include "stdafx.h"
#include "Utility.h"    

// ----------------------------------------------------------------------------
// trim()
// Removes Elements To Trim From Left And Right Side Of The str
std::string Utility::trim(const std::string& str, const std::string elementsToTrim) {
    std::basic_string<char>::size_type firstIndex = str.find_first_not_of(elementsToTrim);
    if (firstIndex == std::string::npos) {
        return std::string(); // Nothing Left
    }

    std::basic_string<char>::size_type lastIndex = str.find_last_not_of(elementsToTrim);
    return str.substr(firstIndex, lastIndex - firstIndex + 1);
} // trim    

// ----------------------------------------------------------------------------
// splitString()
std::vector<std::string> Utility::splitString( const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty ) {
    std::vector<std::string> vResult;
    if ( strDelimiter.empty() ) {
        vResult.push_back( strStringToSplit );
        return vResult;
    }

    std::string::const_iterator itSubStrStart = strStringToSplit.begin(), itSubStrEnd;
    while ( true ) {
        itSubStrEnd = search( itSubStrStart, strStringToSplit.end(), strDelimiter.begin(), strDelimiter.end() );
        std::string strTemp( itSubStrStart, itSubStrEnd );
        if ( keepEmpty || !strTemp.empty() ) {
            vResult.push_back( strTemp );
        }

        if ( itSubStrEnd == strStringToSplit.end() ) {
            break;
        }

        itSubStrStart = itSubStrEnd + strDelimiter.size();
    }

    return vResult;    
} // splitString
#包括“stdafx.h”
#包括“Utility.h”
// ----------------------------------------------------------------------------
//trim()
//从str的左侧和右侧删除要修剪的图元
std::string实用工具::trim(const std::string&str,const std::string elementsToTrim){
std::basic_string::size_type firstIndex=str.find_first_not_of(elementsToTrim);
if(firstIndex==std::string::npos){
返回std::string();//没有剩余内容
}
std::basic_string::size_type lastIndex=str.find_last_not_of(elementsToTrim);
返回str.substr(firstIndex,lastIndex-firstIndex+1);
}//修剪
// ----------------------------------------------------------------------------
//splitString()
std::vector实用工具::splitString(const std::string和strStringToSplit,const std::string和strDelimiter,const bool keepmpty){
std::向量vResult;
if(strDelimiter.empty()){
vResult.推回(strStringToSplit);
返回vResult;
}
std::string::const_迭代器itSubStrStart=strStringToSplit.begin(),itSubStrEnd;
while(true){
itSubStrEnd=搜索(itSubStrStart、STRSTRSTRINGTOSPLIT.end()、STRDELIMITORS.begin()、STRDELIMITORS.end());
std::字符串strTemp(itSubStrStart,itSubStrEnd);
if(keepEmpty | |!strTemp.empty()){
vResult.推回(strTemp);
}
如果(itSubStrEnd==strStrStringTopSplit.end()){
打破
}
itSubStrStart=itSubStrEnd+STRDELIMITOR.size();
}
返回vResult;
}//拆分字符串
这些方法应该对您有所帮助,并且为了使用它们,只需包含标题,并在需要时使用范围解析操作符,因为这些方法是静态的,您不能实例化或定义“实用程序对象”

#包括“stdafx.h”
#包括“Utility.h”
int main(){
std::string myString(“两个单词”);
std::字符串分隔符(“两”);
向量词;
words=Utility::splitString(myString,分隔符);
//或
words=Utility::splitString(myString,std::string(“”));
//或
words=Utility:splitString(myString,“”);
字符串句子(“你好,世界!”);
句子=实用工具::修剪(句子);//第二个参数默认为空白
返回0;
}

现在,您可以修改这些方法以满足您的需要,但这是我用于可重用字符串操作库的一般想法。

好的,对于上下文字符串替换,我将使用regex之类的。您已经向我们描述了您尝试执行的操作,并且向我们展示了一个程序。该程序是否与您描述的不一样?如果没有,那它做什么呢?问题是什么?当我运行它时,它在从文件读取的第一行上给了我一个错误:在抛出“std::out\u of_range”的实例What():basic\u string::at:u n(这是20)>=this->size()(这是20)后调用terminate,而您的循环有固定的条件,它们要么是无限循环,要么是空循环。显示的代码中几乎每一行都是完全错误的。指出每一个bug需要几段时间。我数了至少六个。你需要完全摆脱这个问题,花一两天时间,读你的C++书籍,从头开始。
#include "stdafx.h"
#include "Utility.h"

int main() {
    std::string myString( "TwoWords" );
    std::string delimiter( "Two" );
    std::vector<std::string> words;

    words = Utility::splitString( myString, delimiter );
    // Or
    words = Utility::splitString( myString, std::string( " " ) );
    // Or
    words = Utility:splitString( myString, " " );

    std::string sentence( " Hello World! " );

    sentence = Utility::trim( sentence ); // 2nd Parameter Default To Whitespace

    return 0;
}