C + + string function supplement (type cross, split, merge, slimming)

Source: Internet
Author: User
Tags sprintf

int stringutil::intfromstring (String data)
{
    NOTE Atoi is a non-standard C function
    Return Atoi (Data.c_str ());
}
string stringutil::stringfromint (int data)
{
    Char tmp[11];
    memset (tmp,0,11);
    sprintf (tmp, "%10d", data);
    return string (TMP);
}
Double stringutil::d oublefromstring (String data)
{
    Double tmp;
    SSCANF (Data.c_str (), "%lf", &tmp);
    return TMP;
}
String stringutil::stringfromdouble (Double data)
{
    Char tmp[21];
    memset (tmp,0,21);
    sprintf (tmp, "%20.3LF", data);
    return string (TMP);
}
Float stringutil::floatfromstring (std::string data)
{
    float tmp;
    SSCANF (Data.c_str (), "%f", &tmp);
    return TMP;
}
std::string stringutil::stringfromfloat (float data)
{
    Char tmp[21];
    memset (tmp,0,21);
    sprintf (tmp, "%20.3f", data);
    return string (TMP);
}
BOOL Stringutil::boolfromstring (std::string data)
{
    if (Data.compare ("true") = = 0)
        return true;
    Else
        return false;
}
std::string Stringutil::stringfrombool (bool data)
{
    if (data)
        Return string ("true");
    Else
        Return string ("false");
}
Vector<std::string> Stringutil::splitstringtoarray (std::string source, std::string seperator)
{
    vector<string> result;
    if (!source.empty ())
    {
        String::size_type begin = 0;
        String::size_type end = 0;
        unsigned int sepsize = Seperator.size ();
        while ((end = Source.find_first_of (Seperator,begin))!=string::npos)
        {
            String item = SOURCE.SUBSTR (Begin,end-begin);
            Result.push_back (item);
            Begin=end + sepsize;
        }
        Last item, note that the final element is an empty string if the last is a separator
        if (Begin <= source.size ())
        {
            String item = SOURCE.SUBSTR (Begin,source.size ()-begin);
            Result.push_back (item);
        }
    }
    return result;
}
std::string stringutil::linkarraytostring (vector<std::string> array, std::string seperator)
{
    string result;
    if (array.size () > 0)
    {
        unsigned int limit = Array.size ()-1;
        for (unsigned int i=0;i< limit;++i)
        {
            Result+=array[i];
            Result+=seperator;
        }
        Result + = Array[limit];
    }
    return result;
}
Map<std::string, std::string> Stringutil::splitstringtomap (std::string source, std::string PRIMARYSEP, std:: String secondarysep)
{
    vector<string> array = Stringutil::splitstringtoarray (SOURCE,PRIMARYSEP);
    Vector<string> Tmparray;
    map<string,string> result;
    for (unsigned int i = 0; i< array.size (); ++i)
    {
        Tmparray = Stringutil::splitstringtoarray (ARRAY[I],SECONDARYSEP);
        if (Tmparray.size () >= 2)
        {
            Result[tmparray[0]] = tmparray[1];
        }
    }
    return result;
}
std::string stringutil::linkmaptostring (map<std::string, std::string> tmpmap, std::string PRIMARYSEP, std:: String secondarysep)
{
    Vector<string> Tmparray;
    String tmpstr;
    Map<string,string>::iterator it = Tmpmap.begin ();
    for (; It!=tmpmap.end (); ++it)
    {
        TMPSTR = it->first+secondarysep+it->second;
        Tmparray.push_back (TMPSTR);
    }
    Return stringutil::linkarraytostring (TMPARRAY,PRIMARYSEP);
}
std::string Stringutil::trimfront (std::string data)
{
    unsigned int i = 0;
    for (; I<data.size () &&data.at (i) = = "; ++i)
    {
    }
    if (I<data.size ())
        Return Data.substr (I,data.size ()-i);
    Else
        return string ("");
}
std::string stringutil::trimback (std::string data)
{
    int i = Data.size ()-1;
    for (; i>=0&&data.at (i) = = ";-I.)
    {
    }
    if (i>=0)
        Return Data.substr (0,i+1);
    Else
        return string ("");
}
std::string Stringutil::trim (std::string data)
{
    string tmp = Stringutil::trimfront (data);
    return Stringutil::trimback (TMP);
}

C + + string function supplement (type cross, split, merge, slimming)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.