当前位置: 技术问答>linux和unix
有关字符串分割的问题
来源: 互联网 发布时间:2017-03-01
本文导语: 假设有如此字符串: aa=11|bb=22|cc=33|dd=44 如果我想得到的结果为: 11,22,33,44 该如何处理,谢谢 | /////////////////////////////////////////////// Spliter ////////////////////////////////////////////// /** * This class used to split...
假设有如此字符串:
aa=11|bb=22|cc=33|dd=44
如果我想得到的结果为:
11,22,33,44
该如何处理,谢谢
aa=11|bb=22|cc=33|dd=44
如果我想得到的结果为:
11,22,33,44
该如何处理,谢谢
|
/////////////////////////////////////////////// Spliter //////////////////////////////////////////////
/**
* This class used to split string according to the delimiter.The string which wanted to be splited shall
* not be changed after splited.
*/
class CSpliter
{
public:
CSpliter(void);
/**
* CSpliter object constructor.
* @param [in] apcDelimiter a pointer pointed to the string which used as delimiter.
* @param [in] abIsSkipBlankField a flag specifies if the null character shall save.
*/
CSpliter( char const* apcDelimiter, bool abIsSkipBlankField = true );
/**
* This function used to specify the delimiter.
* @param [in] apcDelimiter a pointer pointed to the string which used as delimiter.
* @param [in] abIsSkipBlankField a flag specifies if the null character shall save.
*/
void Delimiter ( char const* apcDelimiter, bool abIsSkipBlankField = true );
/**
* This function used to set the preserve area.The area shall identify by the begin and end character.
* @param [in] acStart specifies the start character of the preserve area.
* @param [in] acStop specifies the end character of the preserve area.
* @param [in] abIsStrip specifies if the area's edge shall remain or not.
* @retval 0 successful.
*/
apl_int_t Preserve( char acStart, char acStop, bool abIsStrip = true );
~CSpliter(void);
/**
* This function shall split the string pointed by apcInput.
* @param [in] apcInput a pointer pointed to the string which wanted to be splited.
* @retval >=0 This value specifies the number of parts the string splited.
*/
apl_ssize_t Parse( char const* apcInput );
/**
* Overloaded function.
* @param [in] aoInput the string which wanted to be splited.
* @retval >=0 This value specifies the number of parts the string splited.
*/
apl_ssize_t Parse( std::string const& aoInput );
/**
* This function shall get the number of parts the string which is the first argment of the function Parse() splited.
* @retval >=0 This value specifies the number of parts the string is splited.
*/
apl_size_t GetSize(void) const;
/**
* This function shall get the specific part of the string which has been splited.
* @param [in] aiN specifies the location of the substrig which wanted to be got.
* @retval null Get substring fail.
* @retval >0 Get substring successfully, the return value is a pointer pointed to the substring.
*/
char const* GetField( apl_size_t aiN );
protected:
bool IsDelimiter( char const* apcInput ) const;
bool IsPreserve( char acStart, char& acStop, bool& abIsStrip ) const;
void GetToken( apl_size_t aiFieldID, char const* apcFirst, apl_size_t aiLen );
CSpliter& operator = ( CSpliter const& );
CSpliter( CSpliter const& );
private:
struct CPreserve
{
char mcStart;
char mcStop;
bool mbIsStrip;
};
std::string moDelimiter;
bool mbIsSkipBlankField;
std::vector moPreserves;
std::vector moFields;
apl_size_t muFieldCount;
};
////////////////////////////////////////////////////////////////////////////////////////////
CSpliter::CSpliter(void)
: mbIsSkipBlankField(false)
, moFields(100)
, muFieldCount(0)
{
}
CSpliter::CSpliter( char const* apcDelimiter, bool abIsSkipBlankField )
: mbIsSkipBlankField(false)
, moFields(100)
, muFieldCount(0)
{
this->Delimiter(apcDelimiter, abIsSkipBlankField);
}
CSpliter::~CSpliter(void)
{
}
void CSpliter::Delimiter( char const* apcDelimiter, bool abIsSkipBlankField )
{
this->moDelimiter = apcDelimiter;
this->mbIsSkipBlankField = abIsSkipBlankField ;
}
apl_int_t CSpliter::Preserve( char acStart, char acStop, bool abIsStrip )
{
CPreserve loPreserve;
loPreserve.mcStart = acStart;
loPreserve.mcStop = acStop;
loPreserve.mbIsStrip = abIsStrip;
this->moPreserves.push_back(loPreserve);
return 0;
}
apl_ssize_t CSpliter::Parse( char const* apcInput )
{
char lcStop = 0;
bool lbIsStrip = false;
bool lbIsDualDelimiter = false;
char const* lpcFirst = apcInput;
char const* lpcLast = lpcFirst;
this->muFieldCount = 0;
while (*lpcLast != '')
{
if ( this->IsDelimiter(lpcLast) )
{
if (lpcLast == lpcFirst)
{
if (!this->mbIsSkipBlankField && lbIsDualDelimiter)
{
this->GetToken(this->muFieldCount++, lpcFirst, lpcLast - lpcFirst);
}
//Skip delimiter
lpcLast += this->moDelimiter.length();
lpcFirst += this->moDelimiter.length();
}
else
{
//Get token
this->GetToken(this->muFieldCount++, lpcFirst, lpcLast - lpcFirst);
lpcFirst = lpcLast + this->moDelimiter.length();
lpcLast = lpcFirst;
}
lbIsDualDelimiter = true;
continue;
}
else if ( this->IsPreserve(*lpcLast, lcStop, lbIsStrip) )
{
lbIsDualDelimiter = false;
if (lpcLast != lpcFirst)
{
//Get last token
this->GetToken(this->muFieldCount++, lpcFirst, lpcLast - lpcFirst);
lpcFirst = lpcLast;
}
while( *(++lpcLast) != '' && *lpcLast != lcStop ) {};
if ( *lpcLast == '')
{
if (lbIsStrip)
{
++lpcFirst;
}
continue;
}
if (lbIsStrip)
{
++lpcFirst;
//Get last token
this->GetToken(this->muFieldCount++, lpcFirst, lpcLast - lpcFirst);
lpcFirst = lpcLast + 1;
lpcLast = lpcFirst;
}
else
{
//Get last token
this->GetToken(this->muFieldCount++, lpcFirst, lpcLast - lpcFirst + 1);
lpcFirst = lpcLast + 1;
lpcLast = lpcFirst;
}
continue;
}
lbIsDualDelimiter = false;
lpcLast++;
}
if (lpcLast != lpcFirst)
{
//Get last token
this->GetToken(this->muFieldCount++, lpcFirst, lpcLast - lpcFirst);
}
return this->muFieldCount;
}
apl_ssize_t CSpliter::Parse( std::string const& aoStr )
{
return this->Parse( aoStr.c_str() );
}
bool CSpliter::IsDelimiter( char const* apcInput ) const
{
if (this->moDelimiter.length() > 1)
{
return apl_strncmp( apcInput, this->moDelimiter.c_str(), this->moDelimiter.length() ) == 0 ? true : false;
}
else
{
return apcInput[0] == this->moDelimiter[0] ? true : false;
}
}
bool CSpliter::IsPreserve( char acStart, char& acStop, bool& abIsStrip ) const
{
for ( apl_size_t liN = 0; liN moPreserves.size(); liN++ )
{
if (this->moPreserves[liN].mcStart == acStart)
{
acStop = this->moPreserves[liN].mcStop;
abIsStrip = this->moPreserves[liN].mbIsStrip;
return true;
}
}
return false;
}
void CSpliter::GetToken( apl_size_t aiFieldID, char const* apcFirst, apl_size_t aiLen )
{
if ( aiFieldID >= this->moFields.size() )
{
this->moFields.resize(aiFieldID * 2);
}
this->moFields[aiFieldID].assign(apcFirst, aiLen);
}
apl_size_t CSpliter::GetSize() const
{
return this->muFieldCount;
}
char const* CSpliter::GetField( apl_size_t aiN )
{
return aiN moFields.size() ? this->moFields[aiN].c_str() : NULL;
}
//////////////////////////////////////////////////////////////////////////////////////////////
仅供参考!
|
echo $A|sed -e s/[a-z=]//g -e s/|/,/g
|
#/bin/bash
result=""
str="aa=11|bb=22|cc=33|dd=44"
strs=$(echo ${str} | sed s/|/ /g)
for res in ${strs}
do
ult=$(echo ${res} | awk -F '=' '{print $2}')
if [ "${result}" ]
then
result=${result},${ult}
else
result=${ult}
fi
done
echo ${result}
result=""
str="aa=11|bb=22|cc=33|dd=44"
strs=$(echo ${str} | sed s/|/ /g)
for res in ${strs}
do
ult=$(echo ${res} | awk -F '=' '{print $2}')
if [ "${result}" ]
then
result=${result},${ult}
else
result=${ult}
fi
done
echo ${result}
|
shell的话
echo ${aa} | sed s/|/,/g
echo ${aa} | sed s/|/,/g
|
[nicenight@CSDN ~]$ a="aa=11|bb=22|cc=33|dd=44"
[nicenight@CSDN ~]$ echo $a
aa=11|bb=22|cc=33|dd=44
[nicenight@CSDN ~]$ echo ${a//[a-z=]/} | tr "|" ","
11,22,33,44
|
$ echo "aa=11|bb=22|cc=33|dd=44"|awk -F'[^0-9]+' '{print $2","$3","$4","$5}'
11,22,33,44
11,22,33,44