Removing everything after the character (as well as the character)

I have a line like this:

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

I need to get only "xjdfhfakdjs", but the string is dynamic, not hardcoded, so I don’t know what it is, length, etc., so I wanted to delete everything after%, as well as% char.

How can i do this?

+5
source share
3 answers

std :: string mystr = string1.substr (0, string1.find ("%", 0));

I think this will work.

+1
source
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.

+16
source

C- , , , , , , :

#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);
    }
}

.

0
source

All Articles