Removing everything after character (and also character)

前端 未结 3 1928
星月不相逢
星月不相逢 2021-01-02 01:16

I have a string like this:

std::string string1 = \"xjdfhfakdjs%54k34k.-jk34\";

I need to get only \"\"xjdfhfakdjs\", but the string is dyna

相关标签:
3条回答
  • 2021-01-02 02:05

    I believe that will work.

    std::string mystr = string1.substr(0, string1.find("%", 0));
    
    0 讨论(0)
  • 2021-01-02 02:14

    Did a C-like thing which does work in its current form, but certainly more clunky than the methods shown above:

    #include <windows.h>
    void main ()
    {
        int i;
        wchar_t searchString[100];
        wchar_t * stringReturn;
        memset(searchString, L'\0', sizeof(stringReturn));
        wcscpy_s(searchString, 100, L"String\\ to search");
        stringReturn = wcschr (searchString, '\\');
        if (stringReturn)
        {
            for (i = 0; i < (int)(stringReturn - searchString); i++) stringReturn[i] = searchString[i];
            stringReturn[i] = L'\0';
            wcscpy_s(searchString, 100, stringReturn);
        }
    }
    

    Can be easily modified to work for dynamic strings.

    0 讨论(0)
  • 2021-01-02 02:16
    std::string the_prefix_you_want = string1.substr(0, string1.find("%"));
    

    See: http://www.cplusplus.com/reference/string/string/find/ and http://www.cplusplus.com/reference/string/string/substr/ for more details

    0 讨论(0)
提交回复
热议问题