CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    hello new to this forum

    iv'e been looking for a good forum

    so my first question
    can anyone tell me if there is a helper class for string builder
    that lets you easily and safely pull appended lines
    out of a StringBuilder object into another or gives more of the methods
    that a regular string has like split and such

    i cant seem to get a reply or even a comment on other forums
    other then stack overflow were it was just a snotty "hummppp"
    so i simplified it and removed my code to the basic question

    also when i first registered the link to the forum tags and code poped up
    now i cant find them were did they go ?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: hello new to this forum

    Quote Originally Posted by willmotil View Post
    iv'e been looking for a good forum

    so my first question
    can anyone tell me if there is a helper class for string builder
    that lets you easily and safely pull appended lines
    out of a StringBuilder object into another or gives more of the methods
    that a regular string has like split and such
    Welcome to the forum.

    I don't know of any helper class that allows you to remove appended lines. If you need this sort of functionality, you'll need to write your own class. Be forewarned that if you need a class that allows you to append and access and modify appended lines, starting with the StringBuilder class may not be the best approach. Instead, you may wish to create a wrapper around List<string> or StringArray and add the functionality you desire.

    Regular string methods aren't available because the intent of the StringBuilder class is to efficiently append strings. As you may know, strings in the String class are immutable, so if you append a string to an existing string, a new String object is created each time which is not very efficient when building up large strings.

    This is why the StringBuilder class, doesn't offer more regular string methods; however, once you've built up the string to how you want it in the StringBuilder, you can call its ToString() method and then use the regular string methods.

    Quote Originally Posted by willmotil View Post
    also when i first registered the link to the forum tags and code poped up
    now i cant find them were did they go ?
    Not sure what you mean here. Maybe the site admins can help?

  3. #3
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: hello new to this forum

    ah thank you it was the faq i found it
    i came up with this method to split strings into another list of string builders
    how does this look

    i could make a overload to accept a sb list too and then just reuse whats in it
    and expand it if necessary i think that would avoid garbage collections

    Code:
            public static List<StringBuilder> StringBuilderSplit(StringBuilder sb_text)
            {
                List<StringBuilder> sb_list = new List<StringBuilder>();
                sb_list.Add(new StringBuilder());
                char line_break_char = '\n';
                if (false == char.TryParse(Environment.NewLine, out line_break_char)) { line_break_char = '\n'; }
                //
                int j = 0;
                for (int i = 0; i < sb_text.Length; i++)
                {
                    if (sb_text[i].CompareTo(line_break_char) != 0)
                    {
                        sb_list[j].Append(sb_text[i]);
                    }
                    else
                    {
                        sb_list.Add(new StringBuilder());
                        j++;
                    }
                }
                return sb_list;
            }

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: hello new to this forum

    It's not clear to me why you want to split each string builder line into an array of StringBuilder objects. Based on your original description it would make sense to split each line and add it to a List<string>.

    Also, if you plan to work with items within the lists (whether stringbuilder or strings), returning a list of items from your static method wouldn't be the most convenient. Instead consider returning a class that helps you access each of the string lines (with the functionality that you described earlier).

  5. #5
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: hello new to this forum

    Quote Originally Posted by Arjay View Post
    It's not clear to me why you want to split each string builder line into an array of StringBuilder objects. Based on your original description it would make sense to split each line and add it to a List<string>.

    Also, if you plan to work with items within the lists (whether stringbuilder or strings), returning a list of items from your static method wouldn't be the most convenient. Instead consider returning a class that helps you access each of the string lines (with the functionality that you described earlier).
    well i think im going to do just that ... (really this is just a prototype method for example)
    i do it as a hobby so i don't have any pro freind's to ask typically i just figure it out


    well i avoided the why
    cause it would go into a ...lot... of details and get long winded

    here is the full story...if your bored and want to read it... you asked

    specifically in my case its a mono-game drawstring problem
    that is subtly complicated because im making it general use
    the final result a call like so ...
    DrawCutTextToRectangle(...,text, rectangle,..,options,...)

    im trying to visually keep text within a changeable rectangle area like a basic text drawing box or button
    when the mouse is over it, i want to see the full text
    so this is sort of like a window to text at times, (the behavior may be changable)
    im trying to make it very generic to different types of ...
    translations, screen resizes, virtual screen proportions and user alterations
    so at times preserve line breaks to square up the text later on or discarding them
    to be in a drawing area , this would all be easier with strings, but....

    there are special considerations

    the method i need to pass to
    is in an api call called spritebatch.DrawString(...,StringBuilder,...,... ect)
    this overload is used to avoid gc problems
    (which iv'e already experience due to not using stringbuilder (lazyness) )
    they can become severe...
    so this is not optimizing it's more necessity
    it seems...
    this is the worst case cause of stutters in xna and mono game
    since this text may be passed many times a second, (60 times typically)
    or once with a certain technique... that would not be a general use case to the api calls

    here is the class containing the call above that text will be passed to in order to draw ,
    it takes a single stringbuilder
    https://github.com/mono/MonoGame/blo...SpriteBatch.cs
    which then calls this...
    internal void DrawInto(..., charsource ,....,...)
    https://github.com/mono/MonoGame/blo.../SpriteFont.cs
    it will handle newlines ,
    that can actually be a problem
    because in resizing the displayed width of all lines, with just one stringbuilder
    i need to call spritebatch.drawstring once per line
    so with a list or array, i can just pull the length back in each stringbuilder after a resize
    that is for the rectangle width then draw each sb in the list
    for the height, i can simply not draw a line of text
    that is beyond a display area's rectangle height

    another problem...
    if the user resizes (me) or i have rectangle text box i can make larger smaller
    then this method will need to be recalled, when the text expands (the rectangle width increases)
    so the idea of
    how to properly get lines out of a single stringbuilder and pass them resized is critical.

    this method would not be called probably to much it is a prototype,
    to other reusable one's (those might be)
    but the elements of this list ... of stringbuilder's would (60 frames per second)
    i want it to work good enough eventually as a demo to others
    but especially to myself

    to address arrays. i wanted that originally but... again problem....
    if i do it with just arrays
    1) ill have to double loop it in the algorithm (not that big a deal)
    but
    2) if someone adds to a full line , i would have to toss the array and replace it essentially
    so i choose list

    well there is no deadline so i have plenty of time to think
    (i program like a turtle, sometimes i get over cautious, sometimes i just run in full turtle speed)
    Last edited by willmotil; April 14th, 2014 at 05:52 AM. Reason: ah it didn't format like i thought it would

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: hello new to this forum

    Okay. Just make a class that wraps the StringBuilder and expose the methods you need.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured