Removing everything after character (and also character)
Asked Answered
N

5

15

I have a string 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 is it, the length etc. so I wanted to remove everything after %, and also the % char.

How could I do this?

Nimble answered 1/5, 2012 at 1:11 Comment(1)
Have you given anything a try yet? It's generally a better question (and shows a good faith effort on your part) if you can say, "Here's what I've tried".Epode
V
8

I believe that will work.

std::string mystr = string1.substr(0, string1.find("%", 0));
Varlet answered 1/5, 2012 at 1:19 Comment(0)
T
24
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

Treacherous answered 1/5, 2012 at 1:18 Comment(3)
Whether this is better depends whether you want a new string or not (i.e., if you want to still have access to the original string). If not, then chris's answer is good.Treacherous
I find this much less of a hassle to use. The time taken to copy probably isn't very important (at least the OP didn't mention anything).Seed
More important than performance, chris's string1.erase (string1.find("%"), string1[string1.length() - 1]);, given an empty string1, indexes string1[-1] which has undefined behaviour (will probably crash the application). erase's second argument actually takes a number of characters, with the npos sentinel value indicating the rest of the string, but that's the default anyway so it should be simply string::size_type n = string1.find("%"); if (n != string::npos) string1.erase(n); Edward's answer is safe - if find returns npos substr returns the entire string.Pyrope
V
8

I believe that will work.

std::string mystr = string1.substr(0, string1.find("%", 0));
Varlet answered 1/5, 2012 at 1:19 Comment(0)
A
1

Alternatively.

#include <iostream>
    
int main()
{
    std::string string1 = "xjdfhfakdjs%54k34k.-jk34";
    char findit = string1.find_first_of("%");
    for (int i = findit; i < string1.size(); i++) {
        string1.erase(i);
    }
    std::cout << string1;
}

This will find the first instance from the beginning of the string and then .erase will delete findit and everything after until the .size() is exhausted.

Angele answered 13/12, 2023 at 19:31 Comment(0)
B
0

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.

Brazilin answered 20/2, 2016 at 6:29 Comment(0)
S
-1
string1.erase (string1.find ("%"), string1 [string1.length() - 1]);

That should work.

Seed answered 1/5, 2012 at 1:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.