# TextUtils

Namespace: **Wisej.Base**

Assembly: **Wisej.Framework** (4.0.0.0)

Provides utility methods to measure, encode, and extract mnemonics from text.

{% tabs %}
{% tab title="C#" %}

```csharp
public class TextUtils
```

{% endtab %}

{% tab title="VB.NET" %}

```visual-basic
Public Class TextUtils
```

{% endtab %}
{% endtabs %}

## Fields

| Name                | Type                                                                                                  | Description                                                                                                                             |
| ------------------- | ----------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| **ConvertToString** | [Func\<Object, Object, String>](https://docs.microsoft.com/dotnet/api/system.func-3)                  | <p>Pluggable method used to convert user input to a string.<br><mark style="color:blue;background-color:green;">Since 3.0.10</mark></p> |
| **MeasureString**   | [Func\<String, Font, Size, StringFormat, SizeF>](https://docs.microsoft.com/dotnet/api/system.func-5) | Pluggable method used to measure strings.                                                                                               |

## Methods

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-e4f8ed47a52590951f725a10c269555e81d41d81%2Fstatic.png?alt=media) EscapeText(text, allowHtml)

Escapes special characters in the specified string.

| Parameter                                                                                                                                                                                                                   | Type                                                            | Description                                                           |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- | --------------------------------------------------------------------- |
| **text**                                                                                                                                                                                                                    | [String](https://docs.microsoft.com/dotnet/api/system.string)   | The text to escape.                                                   |
| **allowHtml** ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-723b640963c6cd781ff8c5ce5b24149fa0af7b9c%2Fbadge-optional.svg?alt=media) | [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean) | Indicates that HTML tags should not be escaped. The default is false. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string). The escaped string. Null if the specified string was null, or empty if it was empty.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-e4f8ed47a52590951f725a10c269555e81d41d81%2Fstatic.png?alt=media) EscapeText(text, allowHtml, useMnemonic, translateNewLines)

Removes the ampersand from the text and encodes HTML characters if necessary.

| Parameter             | Type                                                            | Description                                                                                                                                                                          |
| --------------------- | --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **text**              | [String](https://docs.microsoft.com/dotnet/api/system.string)   | The text to convert.                                                                                                                                                                 |
| **allowHtml**         | [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean) | Whether to encode HTML characters.                                                                                                                                                   |
| **useMnemonic**       | [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean) | Whether to convert the first mnemonic to an underlined character.                                                                                                                    |
| **translateNewLines** | [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean) | <p>Whether to convert newlines (CRLF) to \<BR/>. The conversion is performed only when<br>allowHtml is false, or, if<br>allowHtml is true and the text doesn't contain any HTML.</p> |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string). The escaped string. Null if the specified string was null, or empty if it was empty.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-e4f8ed47a52590951f725a10c269555e81d41d81%2Fstatic.png?alt=media) GetMnemonic(text)

Returns the first character that is preceded by an ampersand (&).

| Parameter | Type                                                          | Description                                              |
| --------- | ------------------------------------------------------------- | -------------------------------------------------------- |
| **text**  | [String](https://docs.microsoft.com/dotnet/api/system.string) | The string from which to extract the mnemonic character. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string).

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-e4f8ed47a52590951f725a10c269555e81d41d81%2Fstatic.png?alt=media) MeasureText(text, font)

Returns the size of the specified text when rendered with the specified font.

| Parameter | Type                                                              | Description                                                                                          |
| --------- | ----------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| **text**  | [String](https://docs.microsoft.com/dotnet/api/system.string)     | The text to measure.                                                                                 |
| **font**  | [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font) | The [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font) to apply to the measured text. |

**Returns:** [Size](https://docs.microsoft.com/dotnet/api/system.drawing.size). The computed [Size](https://docs.microsoft.com/dotnet/api/system.drawing.size) of the specified text.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-e4f8ed47a52590951f725a10c269555e81d41d81%2Fstatic.png?alt=media) MeasureText(text, font, format)

Returns the size of the specified text when rendered with the specified font.

| Parameter  | Type                                                                              | Description                                                                                          |
| ---------- | --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| **text**   | [String](https://docs.microsoft.com/dotnet/api/system.string)                     | The text to measure.                                                                                 |
| **font**   | [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font)                 | The [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font) to apply to the measured text. |
| **format** | [StringFormat](https://docs.microsoft.com/dotnet/api/system.drawing.stringformat) | Formatting information.                                                                              |

**Returns:** [Size](https://docs.microsoft.com/dotnet/api/system.drawing.size). The computed [Size](https://docs.microsoft.com/dotnet/api/system.drawing.size) of the specified text.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-e4f8ed47a52590951f725a10c269555e81d41d81%2Fstatic.png?alt=media) MeasureText(text, font, layoutArea)

Returns the size of the specified text when rendered with the specified font.

| Parameter      | Type                                                              | Description                                                                                                      |
| -------------- | ----------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **text**       | [String](https://docs.microsoft.com/dotnet/api/system.string)     | The text to measure.                                                                                             |
| **font**       | [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font) | The [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font) to apply to the measured text.             |
| **layoutArea** | [Size](https://docs.microsoft.com/dotnet/api/system.drawing.size) | The [Size](https://docs.microsoft.com/dotnet/api/system.drawing.size) to use as bounding rectangle for the text. |

**Returns:** [Size](https://docs.microsoft.com/dotnet/api/system.drawing.size).

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-e4f8ed47a52590951f725a10c269555e81d41d81%2Fstatic.png?alt=media) MeasureText(text, font, layoutArea, format)

Returns the size of the specified text when rendered with the specified font.

| Parameter      | Type                                                                              | Description                                                                                                      |
| -------------- | --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **text**       | [String](https://docs.microsoft.com/dotnet/api/system.string)                     | The text to measure.                                                                                             |
| **font**       | [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font)                 | The [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font) to apply to the measured text.             |
| **layoutArea** | [Size](https://docs.microsoft.com/dotnet/api/system.drawing.size)                 | The [Size](https://docs.microsoft.com/dotnet/api/system.drawing.size) to use as bounding rectangle for the text. |
| **format**     | [StringFormat](https://docs.microsoft.com/dotnet/api/system.drawing.stringformat) | Formatting information.                                                                                          |

**Returns:** [Size](https://docs.microsoft.com/dotnet/api/system.drawing.size).

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-e4f8ed47a52590951f725a10c269555e81d41d81%2Fstatic.png?alt=media) MeasureText(text, allowHtml, font, callback)

Returns the size of the specified text when rendered with the specified font.

| Parameter     | Type                                                                   | Description                                                                                          |
| ------------- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| **text**      | [String](https://docs.microsoft.com/dotnet/api/system.string)          | The text to measure.                                                                                 |
| **allowHtml** | [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean)        | The text to measure may contain HTML tags.                                                           |
| **font**      | [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font)      | The [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font) to apply to the measured text. |
| **callback**  | [Action\<Size>](https://docs.microsoft.com/dotnet/api/system.action-1) | Result callback method, invoked when the client has finished measuring the text.                     |

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-e4f8ed47a52590951f725a10c269555e81d41d81%2Fstatic.png?alt=media) MeasureText(text, allowHtml, font, width, callback)

Returns the size of the specified text when rendered with the specified font.

| Parameter     | Type                                                                   | Description                                                                                          |
| ------------- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| **text**      | [String](https://docs.microsoft.com/dotnet/api/system.string)          | The text to measure.                                                                                 |
| **allowHtml** | [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean)        | The text to measure may contain HTML tags.                                                           |
| **font**      | [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font)      | The [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font) to apply to the measured text. |
| **width**     | [Int32](https://docs.microsoft.com/dotnet/api/system.int32)            | The width to use as bounding rectangle for the text.                                                 |
| **callback**  | [Action\<Size>](https://docs.microsoft.com/dotnet/api/system.action-1) | Result callback method, invoked when the client has finished measuring the text.                     |

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-e4f8ed47a52590951f725a10c269555e81d41d81%2Fstatic.png?alt=media) MeasureTextAsync(text, allowHtml, font, width)

Asynchronously returns the size of the specified text when rendered with the specified font. The measurement is performed on the browser. Returns an awaitable [Task](https://docs.microsoft.com/dotnet/api/system.threading.tasks.task) that contains the measured size.

| Parameter                                                                                                                                                                                                               | Type                                                              | Description                                                                                          |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| **text**                                                                                                                                                                                                                | [String](https://docs.microsoft.com/dotnet/api/system.string)     | The text to measure.                                                                                 |
| **allowHtml**                                                                                                                                                                                                           | [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean)   | The text to measure may contain HTML tags.                                                           |
| **font**                                                                                                                                                                                                                | [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font) | The [Font](https://docs.microsoft.com/dotnet/api/system.drawing.font) to apply to the measured text. |
| **width** ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-723b640963c6cd781ff8c5ce5b24149fa0af7b9c%2Fbadge-optional.svg?alt=media) | [Int32](https://docs.microsoft.com/dotnet/api/system.int32)       | The optional width to use as bounding rectangle for the text.                                        |

**Returns:** [Task\<Size>](https://docs.microsoft.com/dotnet/api/system.threading.tasks.task-1). An awaitable [Task](https://docs.microsoft.com/dotnet/api/system.threading.tasks.task) that represents the asynchronous operation.
