日期:2014-05-16  浏览次数:20722 次

有关字符串分割的问题
假设有如此字符串:
aa=11|bb=22|cc=33|dd=44
如果我想得到的结果为:
11,22,33,44
该如何处理,谢谢

------解决方案--------------------
shell的话

 echo ${aa} | sed 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}

------解决方案--------------------
C/C++ code

/////////////////////////////////////////////// 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 mbIsSkipBlankFie