CCStringBuilderUtility

static class

CCStringBuilderUtility is a utility class for efficient and fast StringBuilder manipulation. None of the methods create temporary or persistent objects. Memory allocation only happens when the StringBuilder itself needs more space.

Static Variables

These variables act as global settings that control how numbers are converted to text.
decimalSeparator
char

Character used as decimal separator. Change this if you don't want to use '.'.

groupSeparator
char

Character used as digit group separatator. Change this is you don't want to use a ','.

padding
char

Character used as padding. Change this is you don't want to use '0'.

Static Methods

All methods have a StringBuilder named s as their first parameter. This is the StringBuilder that will be manipulated.
AppendInt
void

Append an int to a StringBuilder and optionally include left padding.

Parameters

s
StringBuilder

The StringBuilder to append to.

number
int

The int to append.

digitCount
int

How many digits should be shown. If number has fewer digits, it will be padded to the left.

This parameter is optional.

AppendIntGrouped
void

Append an int to a StringBuilder and include digit group separators and optionally left padding as well.

Parameters

Same as the AppendInt method.
AppendFloat
void

Append a float to a StringBuilder and optionally include left padding.

Parameters

s
StringBuilder

The StringBuilder to append to.

number
int

The int to append.

decimalCount
int

How many decimals to show.

digitCount
int

How many digits should be shown. If number has fewer digits, it will be padded to the left.

This parameter is optional.

AppendFloatGrouped
void

Append a float to a StringBuilder and include digit group separators and optionally left padding as well.

Parameters

Same as the AppendFloat method.
Reverse
void

Reverse a portion of a StringBuilder.

Parameters

s
StringBuilder

The StringBuilder to modify.

firstIndex
int

Index of the first character to reverse.

lastIndex
int

Index of the last character to reverse.

Example

using System.Text;
using UnityEngine;

public class CCStringBuilderUtilityExample : MonoBehaviour {

	private StringBuilder sb;

	void Start () {
		sb = new StringBuilder(10);
	}

	void Update () {
		sb.Length = 0;
		CCStringBuilderUtility.AppendFloatGrouped(sb, Time.timeSinceLevelLoad * 1000f, 2, 6);
		Debug.Log(sb);
	}
}
The above code defines a component type that constantly debugs how much miliseconds have elapsed since level load. It does this with a float formatted like "000,000.00".

Create a new game object, add the component to it, and enter play mode. You'll see the elapsed time being displayed in the bottom left of the editor window and in the console.

Note that in this example a string object is created each update, because Debug.Log calls the ToString method of the StringBuilder. To prevent constant object creation, use a custom solution or the CCText component.