问个c语言最基础的问题. string StrCom = str2.substr(0,4); //str2是字符串"(com45)"
请问StrCom如何得到这个字符串中两个()之间的字符串?

解决方案 »

  1.   

    string StrCom = str2.substr(1,5);
      

  2.   

    CString str=_T("(com4)");
    str.Replace(_T("("),_T(""));
    str.Replace(_T(")"),_T(""));
      

  3.   

    string StrCom = str2.substr(str2.Find('(') + 1,str2.Find(')') - str2.Find('(') - 1);安全点你可以判断下 Find的返回值 或者用3楼的 不过你要考虑"(sa(asad)as)"等等这些情况下该怎么处理
      

  4.   

    如果括号左右还有东西,就不支持了。
    大致这样就可以(假定字串是规则的,如果字串复杂,要加各种判断):
    #include <string>using namespace std;int main()
    {
        string xxx = "左边的(括号中的部分)右边的";
        int nLeft = xxx.find("(");
        int nRight = xxx.find(")");
        string result = string(xxx.c_str() + nLeft + 1, nRight - nLeft - 1);
        printf("%s\n", result.c_str());
    }
      

  5.   

    如果括号左右还有东西,就不支持了。
    大致这样就可以(假定字串是规则的,如果字串复杂,要加各种判断):
    #include <string>using namespace std;int main()
    {
        string xxx = "左边的(括号中的部分)右边的";
        int nLeft = xxx.find("(");
        int nRight = xxx.find(")");
        string result = string(xxx.c_str() + nLeft + 1, nRight - nLeft - 1);
        printf("%s\n", result.c_str());
    }

    如果是复杂的最好用正则表达式