Pick once piece of text out of a string
Pick once piece of text out of a string
I need to assign a string from a specific point in another string.
NOTE: the text and length changes, the only thing that is constant are the delimiters of |A|, |B|, |C|, |D|.
say I have received a sting of "|A|Hello|B|Big|C|Blue|D|World" and placed that into string_a
Now, I want string_b to be set to "Blue".
I can use "loc_start = strstr(newstring,"|C|") + 3 " to find the start of the text location of |C|.
and use "loc_end = strstr(newstring,"|D|") to find the start of the next delimiter.
loc_diff = loc_end - loc_start;
so now I know how long the string is with loc_diff, how can simply copy loc_diff characters in a string starting at loc_start ?
am I stuck using a for loop and strcpy ?
Or is there some C Function call I missed named something like:
strcpynton(char *destination, char * source, size_t start_pos, size_t end_pos);
NOTE: the text and length changes, the only thing that is constant are the delimiters of |A|, |B|, |C|, |D|.
say I have received a sting of "|A|Hello|B|Big|C|Blue|D|World" and placed that into string_a
Now, I want string_b to be set to "Blue".
I can use "loc_start = strstr(newstring,"|C|") + 3 " to find the start of the text location of |C|.
and use "loc_end = strstr(newstring,"|D|") to find the start of the next delimiter.
loc_diff = loc_end - loc_start;
so now I know how long the string is with loc_diff, how can simply copy loc_diff characters in a string starting at loc_start ?
am I stuck using a for loop and strcpy ?
Or is there some C Function call I missed named something like:
strcpynton(char *destination, char * source, size_t start_pos, size_t end_pos);
Re: Pick once piece of text out of a string
You can use strncpy().
Re: Pick once piece of text out of a string
how can i tell strncpy to start at a certain position ?
when i read the desc. of it it starts at loc 0 of the string.
when i read the desc. of it it starts at loc 0 of the string.
Re: Pick once piece of text out of a string
i got it i did not know you could do this for strncpy.
strncpy(desination, &source[x], x);
however, though the above works, how do i replace &source[x] with the starting position i want from the loc_start as it is a char * ?
strncpy(desination, &source[x], x);
however, though the above works, how do i replace &source[x] with the starting position i want from the loc_start as it is a char * ?
Last edited by seulater on Tue Oct 21, 2008 10:04 am, edited 1 time in total.
- Chris Ruff
- Posts: 222
- Joined: Thu Apr 24, 2008 4:09 pm
- Location: topsail island, nc
- Contact:
Re: Pick once piece of text out of a string
dont forget the termination!
strncpy(outstr,&instr[x],y);
outstr[y] = 0;
strncpy does not terminate
Chris
strncpy(outstr,&instr[x],y);
outstr[y] = 0;
strncpy does not terminate
Chris
Real Programmers don't comment their code. If it was hard to write, it should be hard to understand
Re: Pick once piece of text out of a string
yes, but my problem is the "&instr[x]" how am i getting the [x] ?
i cannot use x = strstr(newstring,"|B|")
as strstr returns a char *
i cannot use x = strstr(newstring,"|B|")
as strstr returns a char *
Re: Pick once piece of text out of a string
well, if anyone cares, this is how i did it.
char *loc_start, *loc_end;
int x;
char Unit_ID_Name[25];
loc_start = (strstr(newstring,"|A|")) + 3;
loc_end = strstr(newstring,"|B|");
x = loc_end - loc_start;
strncpy(Unit_ID_Name,loc_start, x);
Unit_ID_Name[x] = 0;
Thanks for all the help, it led me to this solution.
char *loc_start, *loc_end;
int x;
char Unit_ID_Name[25];
loc_start = (strstr(newstring,"|A|")) + 3;
loc_end = strstr(newstring,"|B|");
x = loc_end - loc_start;
strncpy(Unit_ID_Name,loc_start, x);
Unit_ID_Name[x] = 0;
Thanks for all the help, it led me to this solution.
Re: Pick once piece of text out of a string
probably a bit late but I found this to be an easy way to parse your string, and if you wanted to throw it in a function to parse the entire string:
And you could call it like so:
or if you want it exactly how you have it, to just get |B| value:
5 lines and a bit easier to read. You can use strings with NBEclipse, so might as well, easier to do this kind of thing with.
Heck if your input string of "|A|Hello|B|Big|C|Blue|D|World" is a char[x] or char* you can always do this:
Know it's a bit late but just some thoughts.
Code: Select all
string parseIt(const char * start,const char * end,string& data)
{
size_t st, fin;
string value;
st = data.find(start, 0);
if(end == "")
{
fin = data.find(start, string::npos);
}
fin = data.find(end, st);
value = data.substr(st+2, fin-(st+2));
return value;
}
Code: Select all
Unit_ID_Name = parseIt("|A|", "|B|", parseMe)
Unit_ID_Name = parseIt("|B|", "|C|", parseMe)
Unit_ID_Name = parseIt("|C|", "|D|", parseMe)
Unit_ID_Name = parseIt("|D|", "", parseMe)
or if you want it exactly how you have it, to just get |B| value:
Code: Select all
size_t st, fin;
string value;
st = string_a.find("|A|", 0);
fin = string_a.find("|B|", st);
value = string_a.substr(st+3, fin-(st+3));
Heck if your input string of "|A|Hello|B|Big|C|Blue|D|World" is a char[x] or char* you can always do this:
Code: Select all
char* buff = "|A|Hello|B|Big|C|Blue|D|World"; // or however it gets assigned
string string_a = string(buff);