ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 문자열 예제
    C# 2021. 10. 2. 17:35

     

     

    Substring - 일부분 가져오기 string s3 = "Visual C# Express";
    System.Console.WriteLine(s3.Substring(7, 2));
    // Output: "C#"
    String[] pairs = { "Color1=red", "Color2=green", "Color3=blue",
                     "Title=Code Repository" };
    foreach (var pair in pairs)
    {
        int position = pair.IndexOf("=");
        if (position < 0)
            continue;
        Console.WriteLine("Key: {0}, Value: '{1}'",
                       pair.Substring(0, position),
                       pair.Substring(position + 1));
    }

    // The example displays the following output:
    //     Key: Color1, Value: 'red'
    //     Key: Color2, Value: 'green'
    //     Key: Color3, Value: 'blue'
    //     Key: Title, Value: 'Code Repository'
    문자열 바꾸기

    System.Console.WriteLine(s3.Replace("C#", "Basic"));
    // Output: "Visual Basic Express"
    string source = "The mountains are behind the clouds today.";

    // Replace one substring with another with String.Replace.
    // Only exact matches are supported.
    var replacement = source.Replace("mountains", "peaks");
    Console.WriteLine($"The source string is <{source}>");
    Console.WriteLine($"The updated string is <{replacement}>");
    문자열 있는 인덱스 가져오기 // Index values are zero-based
    int index = s3.IndexOf("C");
    // index = 7
    문자열 분리 string phrase = "The quick brown fox jumps over the lazy dog.";
    string[] words = phrase.Split(' ');
    foreach (var word in words)
    {
        System.Console.WriteLine($"<{word}>");
    }

     

    'C#' 카테고리의 다른 글

    [WPF] Datagrid 내에서 항목 삭제하기  (0) 2024.05.28
    Stream 에서 한줄씩 읽기  (0) 2021.10.02
    Save file. SaveFileDialog  (0) 2021.10.02
    파일 선택. File picker  (0) 2021.10.02
    파일 읽기, StreamReader  (0) 2021.10.02
Designed by Tistory.