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.
char
Character used as decimal separator.
Change this if you don't want to use '.'
.
char
Character used as digit group separatator.
Change this is you don't want to use a ','
.
char
Character used as padding.
Change this is you don't want to use '0'
.
StringBuilder
named s
as their first parameter.
This is the StringBuilder
that will be manipulated.
void
Append an int
to a StringBuilder
and optionally include left padding.
StringBuilder
The StringBuilder
to append to.
int
The int
to append.
int
How many digits should be shown. If number
has fewer digits, it will be padded to the left.
This parameter is optional.
void
Append an int
to a StringBuilder
and include digit group separators and optionally left padding as well.
AppendInt
method.
void
Append a float
to a StringBuilder
and optionally include left padding.
StringBuilder
The StringBuilder
to append to.
int
The int
to append.
int
How many decimals to show.
int
How many digits should be shown. If number
has fewer digits, it will be padded to the left.
This parameter is optional.
void
Append a float
to a StringBuilder
and include digit group separators and optionally left padding as well.
AppendFloat
method.
void
Reverse a portion of a StringBuilder
.
StringBuilder
The StringBuilder
to modify.
int
Index of the first character to reverse.
int
Index of the last character to reverse.
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.