Skip to content

StringExtensions

Namespace: SideXP.Core

public static class StringExtensions

Extension functions for string values.

Fields

Ascii0

public const byte Ascii0 = 48

Ascii9

public const byte Ascii9 = 57

AsciiA

public const byte AsciiA = 65

AsciiZ

public const byte AsciiZ = 90

Asciia

public const byte Asciia = 97

Asciiz

public const byte Asciiz = 122

Methods

Split(string, string, StringSplitOptions)

public static string[] Split(string str, string separator, StringSplitOptions splitOptions = None)

Polyfill for Split, which was added to Unity's base class library in Unity 2021.2 (.NET Standard 2.1). On Unity 2021.2 and newer, the built-in method is used instead.

Parameters

  • str: The string to split.
  • separator: The separator to use for splitting the string.
  • splitOptions: Eventual split options.

Returns

Returns the splitted string.

Remarks

The default options match the built-in method (None, so empty entries are kept), ensuring that splitting a string behaves identically across all supported Unity versions.

SplitLines(string, StringSplitOptions)

public static string[] SplitLines(string str, StringSplitOptions splitOptions = RemoveEmptyEntries)

Splits a string by newline characters.

Parameters

  • str: The string to split.
  • splitOptions: Eventual split options.

Returns

Returns the splitted string.

Repeat(string, int)

public static string Repeat(string str, int iterations)

Creates a string that contains several iterations of the given input string.

Parameters

  • str: The string to repeat.
  • iterations: The number of times the input string is repeated in the output.

Returns

Returns the input string repeated the given number of times.

Occurrences(string, string)

public static int Occurrences(string str, string pattern)

Counts the number of (non-overlapping) occurrences of a given substring into another string.

Parameters

  • str: The string in which you want to count the occurrences.
  • pattern: The substring you want to count the occurrences of.

Returns

Returns the number of occurrences found.

Remarks

The pattern is matched literally (ordinal), not as a regular expression.

Slice(string, int)

public static string Slice(string str, int start)

Extracts a substring, from an index to another.

Parameters

  • str: The string you want to slice.
  • start: The start index of the string to extract. If negative, it's used as (str.Length - start).

Returns

Returns the substring, from the given start index to the end of the string.

Slice(string, int, int)

public static string Slice(string str, int start, int end)

Extracts a substring, from an index to another.

Parameters

  • str: The string you want to slice.
  • start: The start index of the string to extract. If negative, it's used as (str.Length - start).
  • end: The end index of the string to extract (exclusive). If negative, it's used as (str.Length - end). If the end index is lower than the start index, start and end are inverted.

Returns

Returns the substring, from the given start index to the given end one.

RemoveDiacritics(string)

public static string RemoveDiacritics(string str)

Remove all diacritic characters (accents, combined letters, emojis, ...), keeping their base character in the output string.

Parameters

  • str: The string from which you want to remove the diacritic characters.

Returns

Returns the processed string.

RemoveSpecialChars(string, string)

public static string RemoveSpecialChars(string str, string allowedChars = null)

Removes all characters that are not letters or digits in the given string.

Parameters

  • str: The string from which you want to remove the characters.
  • allowedChars: The allowed characters in the output string that won't be removed.

Returns

Returns the processed string.

ToPath(string, bool, char)

public static string ToPath(string str, bool normalize = false, char separator = '/')

Converts the given string into a path, by removing forbidden characters and using a single character as directory separator.

Parameters

  • str: The string to convert.
  • normalize: If enabled, the path is resolved to its full path (in the editor, this also resolves Packages/... virtual paths to their actual folder).
  • separator: The character to use as directory separator.

Returns

Returns the converted string.

Example

PathUtility.ToPath("Assets\\Foo\\Bar");                // "Assets/Foo/Bar"
PathUtility.ToPath("Assets/Foo/Bar", separator: '\\'); // "Assets\Foo\Bar"

ToAbsolutePath(string)

public static string ToAbsolutePath(string str)

If the given string represents a relative path, it's combined with ProjectPath to make it absolute.

Returns

Returns the absolute path string.

Example

// Assuming the project is at "C:/dev/MyProject":
PathUtility.ToAbsolutePath("Assets/Foo"); // "C:/dev/MyProject/Assets/Foo"
PathUtility.ToAbsolutePath("");           // the project path

ToRelativePath(string, bool)

public static string ToRelativePath(string str, bool normalize = false)

If the given string represents an absolute path leading to this project, this function makes it relative to ProjectPath.

Returns

Returns the relative path string.

Example

// Assuming the project is at "C:/dev/MyProject":
PathUtility.ToRelativePath("C:/dev/MyProject/Assets/Foo"); // "Assets/Foo"
PathUtility.ToRelativePath("Assets/Foo");                  // "Assets/Foo" (already relative)

IsProjectPath(string)

public static bool IsProjectPath(string str)

Checks if the given path leads to this project.

Returns

Returns true if the given path leads to this project.

Example

PathUtility.IsProjectPath("Assets/Foo");                  // true (relative paths are project paths)
PathUtility.IsProjectPath("C:/dev/MyProject/Assets/Foo"); // true
PathUtility.IsProjectPath("C:/Windows/System32");         // false

ToGUIContent(string, Texture2D, string)

public static GUIContent ToGUIContent(string text, Texture2D icon, string tooltip)

Converts this string into a GUIContent instance.

Parameters

  • text: The text of the GUIContent.
  • icon: The icon of the GUIContent.
  • tooltip: The tooltip of the GUIContent.

Returns

Returns the converted GUIContent.

ToGUIContent(string, Texture2D)

public static GUIContent ToGUIContent(string text, Texture2D icon)

Converts this string into a GUIContent instance.

Parameters

  • text: The text of the GUIContent.
  • icon: The icon of the GUIContent.

Returns

Returns the converted GUIContent.

ToGUIContent(string, string)

public static GUIContent ToGUIContent(string text, string tooltip)

Converts this string into a GUIContent instance.

Parameters

  • text: The text of the GUIContent.
  • tooltip: The tooltip of the GUIContent.

Returns

Returns the converted GUIContent.

ToGUIContent(Texture2D, string)

public static GUIContent ToGUIContent(Texture2D icon, string tooltip)

Converts this string into a GUIContent instance.

Parameters

  • icon: The icon of the GUIContent.
  • tooltip: The tooltip of the GUIContent.

Returns

Returns the converted GUIContent.

ToGUIContent(string)

public static GUIContent ToGUIContent(string text)

Converts this string into a GUIContent instance.

Parameters

  • text: The text of the GUIContent.

Returns

Returns the converted GUIContent.