How to truncate a string [formating] ? c++
Asked Answered
R

2

9

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)

Reconstructive answered 25/10, 2013 at 4:2 Comment(2)
The... wget function?Pyridoxine
I believe you mean setw() not wget() ...?Plutonian
F
7

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
Foiled answered 25/10, 2013 at 13:9 Comment(2)
I feel that there should be a "width-3" for the ellipsis example, otherwise your truncate result exceeds the width.Uralite
It should also be noted that when defining the default for show_ellipsis (when defined in header/source file) the source file should just have bool show_ellipssis without the default value.Buffum
F
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.

Fabiola answered 27/11, 2021 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.