Pick once piece of text out of a string

Discussion to talk about software related topics only.
Post Reply
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Pick once piece of text out of a string

Post 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);
stevep
Posts: 16
Joined: Fri Apr 25, 2008 6:34 am

Re: Pick once piece of text out of a string

Post by stevep »

You can use strncpy().
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Pick once piece of text out of a string

Post 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.
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Pick once piece of text out of a string

Post 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 * ?
Last edited by seulater on Tue Oct 21, 2008 10:04 am, edited 1 time in total.
User avatar
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

Post by Chris Ruff »

dont forget the termination!

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
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Pick once piece of text out of a string

Post 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 *
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Pick once piece of text out of a string

Post 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.
ccoleman
Posts: 9
Joined: Wed Nov 05, 2008 8:55 am

Re: Pick once piece of text out of a string

Post 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.
Post Reply