I have a long string that I want to strip off the end. I want to get rid of everything after the character “<” (inclusively). Here is the code that works:
char *end;
end = strchr(mystring, '<');
mystring[strlen(mystring) - strlen(end)] = '\0';
So if mystring was
"asdfjk234klsjadflnwer023jokmnasdf</tag>alskjdflk23<tag2>akjsldfjsdf</tag2>blabla"
this code would return
"asdfjk234klsjadflnwer023jokmnasdf"
I’m wondering if this can be done in a easier way? I know I can increment a counter over each character in mystring till I find “<” and then used that int as the index, but that seems equally troublesome. All the other built-in string libraries don’t seem useful but I’m sure I’m just looking at this in the wrong way. I haven’t used C for years.
Any help is appreciated!
Sure. This is the idiomatic way to do it: