🎮 Unity 개발/C#

String 문자열

gameuiux 2023. 8. 29. 21:39
728x90
반응형

String 문자열

String을 사용해 문자열 변수를 선언하고, 변수를 조합해서 문자를 출력할 수 있다.

 

s1.Length // 글자 길이
s1.ToLower() // 소문자 변경
s1.ToUpper() // 대문자 변경
s1.IndexOf("m") // 0부터 몇번째인지
s1[2] // 2번째 인덱스에 해당하는 값
s1.Substring(2) // 글자 일부 삭제

// 문자열 비교
s1 == "Player" // True
s2 == "Enemy" // False
s2.Contains("Enemy") // True
s3.StartsWith("En") // True
s1.EndsWith("er") // True

// 특수 문자
"Game\nOver" // new line 줄바꿈 \n
"Game\t\t\tOver" // tab 탭 \t
"\"Good Game\"" // "Good Game" // 순서 \"글자\"
"\'Good Game\'" // 'Good Game'

 

코드

public class _07_String : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string s1 = "Game";
        string s2 = "Start";
        string s3 = "Over";

        Debug.Log(s1 + s2); // GameStart
        Debug.Log(s1 + s3); // GameOver

        // Game Start
        // Game Over
        Debug.Log(s1 + " " + s2);
        Debug.Log(s2 + " " + s3);

        Debug.Log(s1.Length); // 4
        Debug.Log(s1.ToLower()); // 소문자 변경
        Debug.Log(s1.ToUpper()); // 대문자 변경
        Debug.Log(s1.IndexOf("m")); // 0부터 몇번째인지 // 2 
        Debug.Log(s1[2]); // 2번째 인덱스에 해당하는 값 // m
        Debug.Log(s1.Substring(2)); // 글자 일부 삭제 // me 출력

        s1 = "Player";
        s2 = "Enemy1";
        s3 = "Enemy2";

        // 문자열 비교
        Debug.Log(s1 == "Player"); // True
        Debug.Log(s2 == "Enemy"); // False
        Debug.Log(s2.Contains("Enemy")); // True
        Debug.Log(s3.StartsWith("En")); // True
        Debug.Log(s1.EndsWith("er")); // True

        // 특수 문자
        Debug.Log("Game\nOver"); // new line 줄바꿈 \n
        Debug.Log("Game\t\t\tOver"); // tab 탭 \t
        Debug.Log("\"Good Game\""); // "Good Game" // 순서 \"글자\"
        Debug.Log("\'Good Game\'"); // 'Good Game'
    
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

 

결과

 

s1 s2 s3에 각각 string 변수를 선언 후
변수를 조합해 출력한 결과, 문자열 비교 결과, 특수 문자 사용해서 출력한 결과

728x90
반응형