Is there any easy way to get the count of printable characters in a string?

I'm attempting to add some colored console output to an application via the method described in this thread:

This method uses unicode escapes to specify color information directly in the string. For instance, this print statement will print the word "yellow" in hellow text, and then reset the console output to the default:


The issue is, I am also using string counts for formatting, and the escape sequences are also counted in the string length, even if they are not visible. So for instance, the string in the print statement above has a count of 19, even if only 6 characters will be printed.

I've tried using the alternate representations of the string to get a count, but they all seem to return 19:

  1> var s = "\u{001B}[0;33myellow\u{001B}[0;0m"
s: String = "\u{1b}[0;33myellow\u{1b}[0;0m"
  2> s.count
$R0: Int = 19
  3> s.utf16.count
$R1: Int = 19
  4> s.unicodeScalars.count
$R2: Int = 19
  5> s.utf8.count
$R3: Int = 19

So my question is, is there any way to get the count of printable characters in the general case? It seems possible to use a regex or something to strip out these specific color specifiers, but it would be nicer to have a general solution

As a general string:

There are 19 elements in your string.

U+001B is a control character (with unspecified semantics). As a control character, it would be usually classed under the umbrella of “non‐printable”. So stripping those two, your string has 17 “visible characters”, and under normal circumstances, it looks like this: “[0;33myellow[0;0m” (Though if I include U+001B directly using HTML entities—see this page’s source—, Safari still tries to show you it is there: “�[0;33myellow�[0;0m”)

In terminal‐land:

To the terminal, some of those character sequences have particular meanings that describe how the terminal should render it. It is not unlike the difference between the appearance of HTML source code versus a rendered HTML document. If you want to know how many letters an HTML document displays, you cannot simply count the letters in its source; you have to interpret the source code to fully understand which parts are supposed to do what. If you want to know how many characters will be displayed by the terminal, you will have to reimplement the terminal’s rendering logic in order to turn the instruction string into a display string. (Or find a library that will do that for you.)

So, you're playing around with ANSI escape code sequence. I've made a library for that, including a function to strip all the ANSI codes inside a string. You may check my ANSITerminal library (on GitHub). Take a look at stripAttributes(from:) → String function in ansiAttribute.swift file within the Sources folder. It's on the last part of the file. It's pretty simple and straightforward.

Hope it helps.

Terms of Service

Privacy Policy

Cookie Policy