Published on

[Unity] Summary of string functions

Authors
  • Evelyn
    Full-time IndieDev.
    I'm Japanese, so please forgive me if my English is strange.

Please share this article if you like!

In this article, we will introduce the string type, which is a string, and string-based functions.

We'll use string manipulation a lot, so let's work together to learn it!

Table of Contents

How to use string

"string" can be used as follows: "string variable = "character"". If there are no double quotes, an error will occur.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    string character = "hello world";
    Debug.Log(character);
  }
}

The output to the console will look like the following.

hello world

How to convert a number to a string "ToString"

When displaying "int" or "float" numbers in Text, such as when displaying high scores, it is necessary to convert them to the string type.

"ToString()" method can be used to convert numbers to strings.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    int num = 123;
    string character = num.ToString();
    Debug.Log(character);
  }
}

The output to the console will look like the following.

123

How to convert a string into numbers "Parse"

Parse("123")" or "float.Parse("123")" to change a string number such as "123" to an "int" or "float" type. Parse("123")" or "float.Parse("123")".

Parse("123")" or "float.Parse("123")". In the case of this code, if the character is an integer, the method in try will be executed, otherwise an error will occur and catch will be executed for exception handling.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    string character = "100";

    try
    {
      int num = int.Parse(character);
      Debug.Log("Integer: " + num);
    }
    catch
    {
      float num = float.Parse(character);
      Debug.Log("Decimal: " + num);
    }
  }
}

How to get the number of characters "Length"

To get the number of characters, enter something like "variable.Length".

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    string character = "abcdefgh";
    Debug.Log(character.Length);
  }
}

The output to the console will look like the following.

8

String line break method "\n

To break the string to be displayed, enter something like "\n ".

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    string character = "evelyn\ngamedev";
    Debug.Log(character);
  }
}

The output to the console will look like the following.

evelyn
gamedev

Search for the corresponding character in a character and get the number of characters from the beginning "IndexOf"

If you want to specify the character you want to search for in a string and get the number of characters from the beginning of the string if there is a match, you can enter "character". IndexOf ("Character to search")".

However, the count will start from zero." If multiple characters are specified, such as "ABC", the number at the position of the initial "A" of the corresponding character will be returned. If there are multiple sets of "ABC", the one with the smaller count will be given priority.

If you want to search for a string after XX characters, you can enter "IndexOf ("character to search", number of characters)".

If there are no matching characters, -1 will be returned.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    string character = "EvelynGameDev Evelyn";
    int num = character.IndexOf("E");
    Debug.Log("For one character: " + num);

    int num2 = character.IndexOf("Ga");
    Debug.Log("For multiple characters: " + num2);

    int none = character.IndexOf("ka");
    Debug.Log("When there is no corresponding character: " + none);

    int num3 = character.IndexOf("Ev", 5);
    Debug.Log("'Ev' after the second: " + num3);
  }
}

The output to the console will look like the following.

For one character: 0
For multiple characters: 6
When there is no corresponding character: -1
'Ev' after the second: 14

How to extract a part of a string "Substring"

If you want to get the characters from the 00th to the back □□ characters in a string, write "String.Substring(number of start characters, number of characters)".

The number of starting characters is counted from 0 for the first character.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    string character = "evelyngamedevevelyngamedev";
    string part = character.Substring(1, 5);
    Debug.Log(part);
  }
}

The output to the console will look like the following.

velyn

How to split a string "Split"

If you want to split a string by a specified character, you can write "string[] variable = character. Split ('delimiter')".

If you want to specify multiple delimiters, use "string[] variable = character. Split ('delimiter', 'delimiter')" with ",".

Note that the delimiters are separated by single quotes " ' '".

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    string character = "evelyngamedevevelyngamedev";
    string[] parts = character.Split('l');
    foreach (string part in parts)
    {
      Debug.Log(part);
    }
  }
}

The output to the console will look like the following.

eve
yngamedeveve
yngamedev

Removing part of a string "Remove"

If you want to remove a part of the string, you can enter something like "character.Remove(number of starting characters , number of characters)".

This will remove the string from the specified number of characters by the specified number of characters.

Note that the starting number of characters is counted from zero.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    string character = "evelyngamedevevelyngamedev";
    string removed = character.Remove(1, 5);
    Debug.Log("removed: " + removed);
  }
}

The output to the console will look like the following.

removed: egamedevevelyngamedev

Replaces part of a string with another character "Replace"

To replace a part of a string with another character, enter something like "character.Replace ("string to replace", "string to replace")".

Note that if there are multiple strings to be replaced, they will all be replaced.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    string character = "evelyngamedevevelyngamedev";
    string replaced = character.Replace("ev", "I love GameDev");

    Debug.Log("replaced: " + replaced);
  }
}

The output to the console will look like the following.

replaced: I love GameDevelyngamedI love GameDevI love GameDevelyngamedI love GameDev

How to convert letters to upper/lower case "ToUpper/ToLower"

If you want to capitalize a string, use "String.ToUpper()".

To lowercase a string, use "String.ToLower()" to lowercase the string.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StringTester : MonoBehaviour {
  // Use this for initialization
  void Start ()
  {
    string character = "EvelynGameDev";
    string upper = character.ToUpper();
    Debug.Log("upper: " + upper);
    string lower = character.ToLower();
    Debug.Log("lower: " + lower);
  }
}

The output to the console will look like the following.

upper: EVELYNGAMEDEV
lower: evelyngamedev

Conclusion

I have introduced many methods in this article.

String manipulation is a must in programming, not just in Unity, so please try to learn it.

I hope the content of this article has been helpful to you.

Please share this article if you like!