Anna belly belly hard/C#

[C# 개념] 3.9 문자열 다루기

bibiana 각선행 2023. 6. 29. 17:05
반응형

3.9.1 문자열 안에서 찾기

using static System.Console;



namespace StringSearch
{
    class MainApp
    {
        static void Main(string[] args)
        {
            string greeting = "Good Morning";

            WriteLine(greeting);
            WriteLine();

            //IndexOf()
            WriteLine("IndexOf 'Good' : {0}", greeting.IndexOf("Good"));
            WriteLine("IndexOf 'o' :{0}", greeting.IndexOf('o'));

            //LastIndexOf()
            WriteLine("LastIndexOf 'Good' : {0}", greeting.LastIndexOf("Good"));
            WriteLine("LastIndexOf 'o' : {0}", greeting.LastIndexOf("o"));

            //StartsWith()
            WriteLine("StartsWith 'Good' : {0}", greeting.StartsWith("Good"));
            WriteLine("StartsWith 'Morning' : {0}", greeting.StartsWith("Morning"));

            //EndsWith()
            WriteLine("EndsWith 'Good' : {0}", greeting.EndsWith("Good"));
            WriteLine("EndsWith 'Morning' : {0}", greeting.EndsWith("Morning"));

            // Contains()
            WriteLine("Contains 'Good' : {0}", greeting.Contains("Good"));
            WriteLine("Contains 'Morning' : {0}", greeting.Contains("Morning"));

            // Replace()
            WriteLine("Replaced 'Moring' with 'Evening':{0}", greeting.Replace("Morning", "Evening"));



        }
    }
}

[

 

3.9.2 문자열 변형하기

- string 형식은 문자열 중간에 또 다른 문자열을 삽입하거나 특정 부분을 삭제하는 등의 작업을 수행하는 메소드 제공

- 대문자/소문자로의 변환 메소드와 문자열 앞/뒤에 있는 공백을 제거하는 메소드도 제공

using static System.Console;

namespace StringModify
{
    class MainApp
    {
        static void Main(string[] args)
        {
            WriteLine("ToLower() : '{0}'","ABC".ToLower());
            WriteLine("ToUpper() : '{0}'", "abc".ToUpper());

            WriteLine("Insert() : '{0}'", "Happy Friday!".Insert(5,"Sunny"));
            WriteLine("Remove() : '{0}'", "I Don't Love You.".Remove(2,6));

            WriteLine("Trim() : '{0}'", "No Spaces".Trim());
            WriteLine("TrimStart() : '{0}'", "No Spaces".TrimStart());
            WriteLine("TrimEnd() : '{0}'", "No Spaces".TrimEnd());
        }
    }
}

3.9.3 문자열 분할하기

 

 

 

 

3.9.4 문자열 서식 맞추기 - Format() 메소드

1) 왼쪽/오른쪽 맞춤

왼쪽부터 채우기
오른쪽 부터 채우기

using System;
using static System.Console;

namespace StringFormatBasic
{
    class MainApp
    {
        static void Main(string[] args)
        {
            string fmt = "{0,-20}{1,-15},{2,30}";

            WriteLine(fmt, "Publisher","Athor","Title");
            WriteLine(fmt, "Marvel", "Stan Lee", "Iron Man");
            WriteLine(fmt, "Habit", "Hyejung Park", "This is C#");
            WriteLine(fmt, "Prentice Hall", "K&R", "The C Programming Language");
        }
    }
}

 

2) 숫자 서식화

using System;
using static System.Console;

namespace StringFormatNumber
{
    class MainApp
    {
        static void Main(string[] args)
        {
            // D : 10진수
            WriteLine("10진수: {0:D}", 123); // WriteLine(string.Format("10진수 :{0,D}",123); 과 동일
            WriteLine("10진수: {0:D5}", 123);
            // X : 16진수
            WriteLine("16진수: {0:X}", 0xFF1234);
            WriteLine("16진수: {0:X8}", 0xFF1234);
            // N : 숫자
            WriteLine("숫자: {0:N}", 123456789);
            WriteLine("숫자: {0:N0}", 123456789); // 자릿수 0 은 소수점 이하를 제거함
            // F : 고정 소수점
            WriteLine("고정 소수점: {0:F}", 123.45);
            WriteLine("고정 소수점: {0:F5}", 123.456);
            // E : 공학용
            WriteLine("공학: {0:E}", 123.456789);

        }
    }
}

 

 

3) 날짜 및 시간 서식화

 

using System;
using System.Globalization;
using static System.Console;

namespace StringFormatDatetime
{
    internal class MainApp
    {
        static void Main(string[] args)
        {
            DateTime dt = new DateTime(2023, 03, 04, 23, 18, 22);

            WriteLine("12시간 형식 : {0:yyyy-MM-dd tt hh:mm:ss (ddd)}",dt);
            WriteLine("24시간 형식 : {0:yyyy-MM-dd HH:mm:ss (dddd)}", dt);

            CultureInfo ciko = new CultureInfo("ko-KR");
            WriteLine();
            WriteLine(dt.ToString("yyyy-MM-dd tt hh:mm:ss (ddd)", ciko));
            WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss (dddd)", ciko));
            WriteLine(dt.ToString(ciko));

            CultureInfo ciEn = new CultureInfo("en-US");
            WriteLine();
            WriteLine(dt.ToString("yyyy-MM-dd tt hh:mm:ss (ddd)", ciEn));
            WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss (dddd)", ciEn));
            WriteLine(dt.ToString(ciEn));

        }
    }
}

 

 

 

4) 문자열 보간

- 보간 : 비거나 누락된 부분을 채운

 

 

using System;
using static System.Console;

namespace StringInterpolation
{
    class MainApp
    {
        static void Main(string[] args)
        {
            string name = "김튼튼";
            int age = 23;
            WriteLine($"{name,-10}, {age:D3}");

            name = "박날씬";
            age = 30;
            WriteLine($"{name}, {age,-10:D3}");

            name = "이비실";
            age = 17;
            WriteLine($"{name}, {(age>20 ? "성임" :"미성년자")}"); ;

        }
    }
}

 

반응형