Skip to main content
gamedev.net
🔒 Locked

Splitting a string

Started by deavik 6 replies 1.1k views
Original Post
deavik
deavik
Hi, I have a long std::string which I want to split in to fixed-size smaller ones. Essentially my goal is displaying the source string in broken lines according to the width of my screen. My code could have many mistakes because I am relatively new to this, please correct me as you feel right. [smile] My string is string error_msg_local, and I have also determined the maximum int chars_on_a_line. Therefore if numlines is the number of lines my text will span,

        numlines = (int) strlen (error_msg_local.c_str()) / chars_on_a_line + 1;

Does this make sense? Also,
    
    char lines[numlines][chars_on_a_line + 1];
Where lines[0], lines[1] will be pointers to the start of each line. At present I am using strncpy (lines[0], error_msg_local.c_str(), chars_on_a_line); but the last character in lines[0] as displayed is a garbage value, which was never there in error_msg_local. I have allotted enough space for the characters, so what is happenning? My next problem is finding out lines[1], lines[2], etc. Is there any function I could use here? I will also eventually run into the problem that words will be broken in half as the string is split up. Is there any way I can make it split only at the ' ' (space) characters? Thanks a lot for any help!
Fruny
Fruny
Why do you use C string manipulation functions on a C++ string?
std::string has plenty of member functions to do that kind of things, like std::string::size(), std::string::substr()...

Secondly, I suspect you've forgotten to set the terminating null character, which does not get added when the string is truncated by the number-of-characters parameter of strncpy.

To split at word boundaries, scan along the string, keeping track of length and last seen space. When the length exceeds the line length, copy up to the last space and restart there, on a new line.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
snk_kid
snk_kid
You need a C++ boost! where you will find all kinds of goodies including:

String Algorithms Library
Boost.Regex & Boost.Xpressive
tokenizer

Among other useful things. If there is a good reason to not use any/all of those then you can build a string tokenizer out of std::basic_string like so:

#include <string>template <	typename OutputIterator,	typename CharT,	typename Traits,	typename Alloc>OutputIteratorstringtok(OutputIterator out,	  const std::basic_string<CharT, Traits, Alloc>& in,	  const CharT* const delimiters) {    typedef typename std::basic_string<CharT, Traits, Alloc>::size_type size_type;    const size_type len = in.length();	size_type i = 0;    while(i < len) {        // eat leading whitespace        i = in.find_first_not_of(delimiters, i);        if(i == std::string::npos)            return out;   // nothing left but white space        // find the end of the token        size_type j = in.find_first_of(delimiters, i);        // push token        if(j == std::string::npos)            return out = in.substr(i);        else {            out = in.substr(i, j-i);            ++out;        }            // set up for next loop        i = j + 1;    }	return out;}#include <iterator>#include <algorithm>#include <deque>#include <iostream>int main() {    std::string sentance("This is a sample string\njust testing\nnothing fancy");   typedef std::deque<std::string> tokens;   tokens t;      stringtok(std::back_inserter(t), sentance, " \t\n");   std::copy(t.begin(), t.end(),             std::ostream_iterator<std::string>(std::cout, "\n"));}


[Edited by - snk_kid on October 13, 2005 6:18:11 AM]
deavik
deavik
Thanks a LOT for your help, the C++ std::string functions are really amazing! You people must think me an ass for these stupid questions, I'm sorry [sad]. But you know, whenever I google for 'c++ string functions', google leaves out the '++' and I get all the c string functions, [lol]!

Thanks for the great pointers Fruny and snk_kid.
kSquared
kSquared
Quote:
Original post by deavik
Thanks a LOT for your help, the C++ std::string functions are really amazing! You people must think me an ass for these stupid questions, I'm sorry [sad].

As the saying goes, "there are no stupid questions, just stupid people." And you're only stupid if you don't ask. =)
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<
mfawcett
mfawcett
Quote:
Original post by deavik
Thanks a LOT for your help, the C++ std::string functions are really amazing! You people must think me an ass for these stupid questions, I'm sorry [sad]. But you know, whenever I google for 'c++ string functions', google leaves out the '++' and I get all the c string functions, [lol]!

Thanks for the great pointers Fruny and snk_kid.

For more ideas on all the different ways you could accomplish the task, see this recent gamedev thread.
--Michael Fawcett
Zahlman
Zahlman
Quote:
Original post by deavik
Thanks a LOT for your help, the C++ std::string functions are really amazing! You people must think me an ass for these stupid questions, I'm sorry [sad]. But you know, whenever I google for 'c++ string functions', google leaves out the '++' and I get all the c string functions, [lol]!

Thanks for the great pointers Fruny and snk_kid.


Try "STL string functions".

Or, just keep Teh Reference(TM) bookmarked. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.