The Char function is a very helpful function. It takes a numeric ASCII code and returns the character representation.
The official online documentation is very good, and describes how we can build a character map.
For Power Apps builders, a common requirement is to determine the ASCII number that corresponds to a given character. The following tables beneath provide a reference guide.
What are some use case scenarios for Char?What exactly can we do with the Char function? The most common scenarios include the following:
1. Adding line breaks in formula When we build text with formula, we can call Char(10) and Char(13) to insert line breaks.
Char(10) is the Line Feed character and Char(13) is the Carriage Return character. Historically, the Line Feed character moves the insertion point to the next line, but doesn't return the cursor to left-most part of the screen. The Carriage Return character returns the cursor to the left.
In practice, it doesn't make a noticeable difference if we use either Char(13) or Char(10) to add line breaks to text on a Power Apps screen. However, it's generally best practice to insert line breaks by inserting Char(13) or Char(10), most notably in cases where we build construct text for use in applications outside of Power Apps.
As an example, let's suppose we want to display a label that summarises text box entries on a screen. We can combine the value from the text input boxes and append lines between each field like so:
2. Displaying special characters
We can call the Char function to display special characters - for example, mathematical symbols, fractional digits, the copyright symbol etc. Whilst the HTML text control provides a simple way to display special characters, The Char function offers a viable way to build text through formula, and to display the output in places such as buttons, or data table headers.
The screenshot beneath demonstrates how to display the 'squared' symbol.
3. Adding non-breaking spaces in formula
Power Apps renders as HTML in a web browser. The behaviour that is familiar to many web developers, is that HTML prevents consecutive whitespace characters by collapsing multiple spaces into a single space.
Let's take the example of a button control. If we set button text to "Save Record" (note the multiple spaces), the button control collapses all consecutive spaces to a single space.
To display multiple consecutive spaces, we can insert non-breaking space characters ( which is Char(160)), as highlighted in the screenshot beneath.
Note that in the canvas app designer, if we click the visual designer, edit the button text, and enter space characters, the designer automatically inserts non-breaking spaces. If we enter spaces through formula or the text property however, Power Apps inserts breaking spaces instead.
ConclusionThis post summarised the usage of the Char function and provided some practical examples of how to utilise the function.