GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Jan-2009, 13:43
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 117
TransformedBG is on a distinguished road

String copy removing XML tags...


Okay so the short and sweet im trying to write a C++ function that will remove the close tokens form an XML Document. Ie:</close> = input; output should = close. I wrote this quick function however i cant get the close to transfer to the output string. any ideas??

CPP / C++ / C Code:
string removeCloseTokens(string in){
    string out; //string to be returned
    int i = 0;
    int size = 0;
    size = in.size(); // mesures size of string
    size -= 3; // take away '>' char and NULL terminator, takes to last letter

    while(i < size){ // i know this could be a for loop, but i cant even get it to work this way
        cout << "the in string contains: " << in[i+2] << endl
        << "the out string contians: " << out[i] << endl;
        out[i] = in[i+2]; //start the in string on 3rd spot to remove "</"
        i++; // increment i
        out[i] = NULL; // set last letter to NULL
    }

    return out;
}
  #2  
Old 15-Jan-2009, 14:13
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: String copy removing XML tags...


std::string.size() returns the size of the string not including the null terminator (the string class doesn't consider any null terminator in the functions exposed to the user).

If you need to remove the first and the last chars, what is the loop for?
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 15-Jan-2009, 14:19
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: String copy removing XML tags...


Quote:
Originally Posted by LuciWiz
If you need to remove the first and the last chars, what is the loop for?

Oh, you were copying each character in the second string. Well, since you are using C++, it can be as easy as:

CPP / C++ / C Code:
string removeCloseTokens(const string & inputString)
{
	string outputString = inputString;

 	outputString.erase(outputString.begin());	   ///< remove '<'
 	outputString.erase(outputString.begin());	   ///< remove '/'
 	outputString.erase(outputString.size() - 1);       ///< remove '>'

	/*
	// or
	outputString.erase(0, 2);			///< remove '</'
	outputString.erase(outputString.size() - 1);	///< remove '>'
	*/

	return outputString;
}

Best regards,
Lucian

Edit: pass the input string by const reference...
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 15-Jan-2009, 15:02
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 117
TransformedBG is on a distinguished road

Re: String copy removing XML tags...


Sweet didn't think of that... I have to remember all that its been about a year lol...
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation to create a Tax Liability info output ThatDudeDave Java Forum 2 10-Dec-2008 07:53
Multiple questions for C++ project devster420 C++ Forum 1 20-Apr-2007 22:26
Message Class TransformedBG C++ Forum 5 29-Nov-2006 22:28
string copy using pointers gemini_v440 C Programming Language 1 18-Mar-2006 11:20
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 09:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:50.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.