Cut utf-8 chars by specified len

Cut utf-8 chars by specified len
0.0 0


Hi, I need to cut utf-8 nicknames (rus,china…) to max specified len. With std:: and ascii chars is easy but not with utf-8 (multibyte chars). I found in CCLabelBMFont function cc_utf8_strlen and other great functions “cc_utf …”to work with multibyte chars. They are not declared in the .h but in the .cpp so CCLabelBMFont::cc_utf8_strlen and other cc_utf functions can’t be called. Copy them is one solution :wink:

Can anyone use these functions?


Hi, these functions are only used for engine. So if you really want to use it, just copy and rename them. These functions are inspired by glib. Therefore you can also refer to glib.


Thank you, I decide to have better control for MB strings, checking counting etc. I decide to use UTF-8 with C*+ in a Portable Way
Let me know if someone have better solution for multibyte strings.
My code to cut long multibyte nicknames:
#include “utf8.h”
std::string username = getNickname; // multibyte nickame
char str = username.c_str; // utf-8 string
str_i = str; // string iterator
char *end = str* strlen(str) + 1; // end iterator

std::string shortUsername; // result

int strCnt = 0; // utf char counter

uint32_t code = utf8::next(str_i, end);

if (code 0)


                if (strCnt  18) // my maximum nickname len

shortUsername = username.substr(0, (username.length() - strlen(str_i))) + “…”; // addd dots after cut
} while (str_i < end);

if (shortUsername.empty()) // if no cut use the org nickname


Sorry to be late to the party but is something like this implemented in current cocos2d-x? Im afraid that C++11 does not work with utf8 strings out of the box,



Yea, for that reason I use mentioned snippet. I don’t see any in cc2dx to cut that strings. Let me know if you find another way to “cut mb strings” in easy way.


I ended up using a code snippet I found on the web. Check out my answer to my question here:

Also there is a std::string-utf8-length checking function within cocos2d-x that will be needed in most cases.

With that said, if using cocos2d-x and utf-8 a lot I guess using a proper library would be best. Ahem… and best of all, if cocos2d-x would scrap C++ one day… or C++ implemented better utf-8 support “natively” in the language.