Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture specified part of a string

Status
Not open for further replies.

cyprus106

Programmer
Apr 30, 2001
654
0
0
I have a String that I need to get only part of.
I can't use any of the regular functions like SubString and such. I need to get the part of the string (named to) in between the "]" and the ":" marks. I know for a fact that both of the characters will be in the string, and in that order. there is a name in between those strings, and for the life of me, I can't seem to get it. Does anyone have a function to do this, or any code examples. I tried doing a SubStr to the ":" character, then making all the characters null ("\0") until the "]", then trim it off... But I'm being retarded today, and when I make one charater null, the rest go with it. I'm out of ideas and fairly frusterated, do you guys have suggestions?

Thanks, Cyprus
[noevil]
 
hhmm

you cant put it into a string?

got 2 mins here

if u can get the position of the ] and the ; cant you just go

substr(positionof],positionof;-positionof]);


I take it no... but then I dont know :)

 
Well, why not just go through the string a character at a time. Start copying the string a character at a time into the return buffer and stop when you hit the :

Matt
 
Zyrenthian,
I had done just that. I went through byte by byte, the same way I always did before, and as soon as I'd make one byte null, it would clear out the whole thing. It never did that before, I can't figure out why it won't work now. I'm sure it's something stupid that I'm doing. If I could remeber an exact project where I had done that before, I wouldn't have to ask, but it seems when I really need it, I can't find it.

LoneRanger,
When I do a substr, I thought that it takes it byte by byte starting at the beginning until it finds a particular character, ":" in this case, and then return that sub string, starting at the beginning. I could very well be wrong. Does it take multiple character values like that? As in tell it where to start, as opposed to the beginning?

Thanks, Cyprus
[noevil]
 
ah, this is AnsiStrings only...

could you load the string into an ansiString, find the middle part, and then pass it back?

or what about this?

TheChars is the char string u are searching
Code:
int count;
int count2;
int len;
char Endstring[200];
BOOL Started;
BOOL Ended;
Started == false;
Ended == false;

len = strlen(TheChars);
count = 0;
count2 = 0;
while (count < len) 
{
  if (Ended == false) { //If Not Finished
     if (Started == false) { //If Not Started
        if (TheChars[count] == &quot;]&quot;) { //If at Start Char
           Started == true;
        }
     } else { //Started

        if (TheChars[count] == &quot;;&quot;) { //If not at end Char
          Ended == true;
        } else {
          EndString[count2] = TheChars[count]; //Add Value
          count2++; //Increment SubString pointer
        }
     }
  }
  count++;
}

[code]

I just made that up on the spot, and I know its longwinded, but im still quite new to this, and thats with my basic knowledge :)
 
This may also give you the desired result ... although, probably not optimized ...
Code:
     char MyStr[200];
     char ResultStr[50];
     char *ptr;
     int Pos1, Pos2;
     strcpy(MyStr, &quot;[Something]NameOfPerson:SomethingElse&quot;);
     ptr = strchr(MyStr, ']');
     Pos1 = ptr-MyStr + 1;
     ptr = strchr(MyStr, ':');
     Pos2 = ptr-MyStr;
     strncpy(ResultStr, MyStr+Pos1, Pos2 - Pos1);
     ResultStr[Pos2 - Pos1] = '\0';
     Application->MessageBoxA(ResultStr, &quot;Debug&quot;, MB_OK);

 
I changed it a bit, removed some variables and now it is also errorproof if the begin- or endchar is not found

char MyStr[200];
char ResultStr[200]={&quot;&quot;};
char *ptr;
strcpy(MyStr, &quot;[Something]NameOfPerson:SomethingElse&quot;);
ptr = strchr(MyStr, ']');
if(ptr)
{ strcpy(ResultStr,ptr+1);
ptr = strchr(ResultStr, ':');
if(ptr)
*ptr=0;
else // if no end is found you can decide here what
// to do, f.i empty the result

ResultStr[0]=0;
}
Application->MessageBoxA(ResultStr, &quot;Debug&quot;, MB_OK);


Wim Vanherp
Wim.Vanherp@belgacom.net
 
thank you very much, wimvanherp (and jnecciai). That did exactly what I wanted.
I appreciate the help, everyone. Cyprus
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top