How to truncate a string [formating] ? c++

23,927

Solution 1

I know you already have a solution, but I thought this was worth mentioning: Yes, you can simply use string::substr, but it's a common practice to use an ellipsis to indicate that a string has been truncated.

If that's something you wanted to incorporate, you could just make a simple truncate function.

#include <iostream>
#include <string>

std::string truncate(std::string str, size_t width, bool show_ellipsis=true)
{
    if (str.length() > width)
        if (show_ellipsis)
            return str.substr(0, width) + "...";
        else
            return str.substr(0, width);
    return str;
}

int main()
{
    std::string str = "Very long string";
    int i = 1;
    std::cout << truncate(str, 8) << "\t" << i << std::endl;
    std::cout << truncate(str, 8, false) << "\t" << i << std::endl;
    return 0;
}

The output would be:

Very lon...   1
Very lon      1

Solution 2

As Chris Olden mentioned above, using string::substr is a way to truncate a string. However, if you need another way to do that you could simply use string::resize and then add the ellipsis if the string has been truncated.

You may wonder what does string::resize? In fact it just resizes the used memory (not the reserved one) by your string and deletes any character beyond the new size, only keeping the first nth character of your string, with n being the new size. Moreover, if the new size is greater, it will expand the used memory of your string, but this aspect of expansion is straightforward I think.

Of course, I don't want to suggest a 'new best way' to do it, it's just another way to truncate a std::string.

If you adapt the Chris Olden truncate function, you get something like this:

#include <iostream>
#include <string>

std::string& truncate(std::string& str, size_t width, bool show_ellipsis=true) {
    if (str.length() > width) {
        if (show_ellipsis) {
            str.resize(width);
            return str.append("...");
        }
        else {
            str.resize(width);
            return str;
       }
    }
    return str;
}

int main() {
    std::string str = "Very long string";
    int i = 1;
    std::cout << truncate(str, 8) << "\t" << i << std::endl;
    std::cout << truncate(str, 8, false) << "\t" << i << std::endl;
    return 0;
}

Even though this method does basically the same, note that this method takes and returns a reference to the modified string, so be careful with it since this string could be destroyed because of an external event in your code. Thus if you don't want to take that risk, just remove the references and the function becomes:

std::string truncate(std::string str, size_t width, bool show_ellipsis=true) {
    if (str.length() > width) {
        if (show_ellipsis) {
            str.resize(width);
            return str + "...";
        }
        else {
            str.resize(width);
            return str;
        }
    }
    return str;
}

I know it's a little bit late to post this answer. However it might come in handy for future visitors.

Share:
23,927
Theriotgames Riot
Author by

Theriotgames Riot

Updated on July 09, 2022

Comments

  • Theriotgames Riot
    Theriotgames Riot almost 2 years

    I want to truncate a string in a cout,

    string word = "Very long word";
    int i = 1;
    cout << word << " " << i;
    

    I want to have as an output of the string a maximum of 8 letters

    so in my case, I want to have

    Very lon 1
    

    instead of :

    Very long word 1
    

    I don't want to use the wget(8) function, since it will not truncate my word to the size I want unfortunately. I also don't want the 'word' string to change its value ( I just want to show to the user a part of the word, but keep it full in my variable)

  • Paul Beckingham
    Paul Beckingham about 8 years
    I feel that there should be a "width-3" for the ellipsis example, otherwise your truncate result exceeds the width.