Page 1 of 1

Pick once piece of text out of a string

Posted: Tue Oct 21, 2008 9:37 am
by seulater
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);

Re: Pick once piece of text out of a string

Posted: Tue Oct 21, 2008 9:55 am
by stevep
You can use strncpy().

Re: Pick once piece of text out of a string

Posted: Tue Oct 21, 2008 9:57 am
by seulater
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.

Re: Pick once piece of text out of a string

Posted: Tue Oct 21, 2008 10:00 am
by seulater
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 * ?

Re: Pick once piece of text out of a string

Posted: Tue Oct 21, 2008 10:04 am
by Chris Ruff
dont forget the termination!

strncpy(outstr,&instr[x],y);
outstr[y] = 0;

strncpy does not terminate


Chris

Re: Pick once piece of text out of a string

Posted: Tue Oct 21, 2008 10:13 am
by seulater
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 *

Re: Pick once piece of text out of a string

Posted: Tue Oct 21, 2008 10:31 am
by seulater
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.

Re: Pick once piece of text out of a string

Posted: Wed Nov 05, 2008 11:08 am
by ccoleman
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:

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;
}
And you could call it like so:

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));
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:

Code: Select all

char* buff =  "|A|Hello|B|Big|C|Blue|D|World"; // or however it gets assigned
string string_a = string(buff);
Know it's a bit late but just some thoughts.