본문 바로가기

   
Programming/C#

string

반응형

문자열(string)

 

문자의 집합(char 무관)

System.String을 상속받음

참조형(레퍼런스 타입 Reference Type)

참조형인데 값형처럼 취급(자주 쓰기 때문에)

string str = "홍길동";

string str = new string("홍길동"); 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace arrange

{

       class String

       {

             public static void Main(string[] args)

             {

                   

                   

 

                    //M1();

                    //M2();            

                    //M3();

                    //M4();

                    //M5();

                    //M6();

                    //M7();

                    //M8();//break point(중단점) : 디버깅을 할때 사용

                    //M9();

                    //M10();

                    //M11();

                    //M12();

                    //M13();

                    //M14();

                    //M15();

                    //M16();

                    M17();

             }

 

             private static void M17()

             {

                    //Trim() : 공백제거

                    string data = "     안녕     홍길동     ";

 

                    Console.WriteLine("+{0}+", data);

                    Console.WriteLine("+{0}+", data.TrimStart());

                    Console.WriteLine("+{0}+", data.TrimEnd());

                    Console.WriteLine("+{0}+", data.Trim());

             }

 

 

private static void M16()

             {

                    string data = "Hello~";

 

                    Console.WriteLine(data.ToUpper());

                    Console.WriteLine(data.ToLower());

             }

 


private static void M15()

             {

                    //Substring() : 추출(문자열의 일부만 반환)

                    string data = "hello- hong-";

 

                    Console.WriteLine(data.Substring(7,4));

                    Console.WriteLine(data.Substring(7));

 

                    string jumin = "841219-1515774";

                    Console.WriteLine(jumin.Substring(7,1));                   

             }

 


private static void M14()

             {

                    //Split() : 자르기

                    string data = "홍길동,이순신,아무게,하하하";

                    string[] result = data.Split(',');

 

                    Console.WriteLine(result.Length);

                    foreach (string name in result)

                    {

                           Console.WriteLine(name);

                    }

             }

 


private static void M13()

             {

                    //Replace() : 치환(바꾸기)

                    string data = "안녕하세요 홍길동님 안녕히 가세요 홍길동 홍길동 짱 짱짱짱 홍길동 홍 길 동 홍길 동";

 

                    //Console.WriteLine(data.Replace("홍길동", "소녀시대"));

                    Console.WriteLine(data.Replace(" ", ""));

 

             }

 


private static void M12()

             {

                    //문자열을 대상으로 특정 단어 1번 출현 -> 검색 삭제 -> 나머지 출력

                    string data = "안녕하세요 홍길동님 안녕히 가세요";

 

                    Console.WriteLine("안녕하세요 홍길동님 안녕히 가세요");

                    Console.Write("삭제할 문자 입력");

                    string word = Console.ReadLine();

 

                    Console.WriteLine(data.Remove(data.IndexOf(word), word.Length));

             }

 


       private static void M11()

             {

                    //Remove() : 문자열의 일부를 삭제

                    string str = "안녕하세요 홍길동님";                 

 

                    Console.WriteLine(str.Remove(2,3));

                    Console.WriteLine(str.Remove(2));

 

             }

 


private static void M10()

             {

                    //PadLeft()

                    string str = "hello";

 

                    Console.WriteLine("+{0}+", str);

                    Console.WriteLine("+{0}+", str.PadLeft(10));

                    Console.WriteLine("+{0}+", str.PadLeft(10, ''));

 

                    string temp = "";

                    Console.WriteLine(temp.PadLeft(30, '-'));

 

                    Console.WriteLine("홍길동".Length);

                    Console.WriteLine("".PadLeft(30, '-'));

             }

 


private static void M9()

             {

                    //IndexOf(), LastIndexOf()

                    string str = "일이삼사오육칠팔구십";

 

                    Console.WriteLine(str.IndexOf(''));

                    Console.WriteLine(str.IndexOf("육칠팔구"));

                    Console.WriteLine(str.IndexOf(""));

 

                    if (str.IndexOf("") > -1)

                    {

                           Console.WriteLine("삼 찾음!!");

                    }

 

                    else

                    {

                           Console.WriteLine("삼 없음!!");

                    }

             }

 


private static void M8()

             {

                    int n = 10;               

                    Console.WriteLine("안녕");

                    n++;

 

                    Console.WriteLine(n);

             }

 

             private static void M7()

             {

                    string[] names = { "홍길동", "이순신", "홍개똥", "홍말똥", "이문세" };

                    for (int i = 0; i < names.Length; i++)

                    {

                           if (names[i].StartsWith(""))

                           {

                                 Console.WriteLine(names[i]);

                           }

                    }

             }

 


private static void M6()

             {

                    //StartsWith(), EndsWith()

                    string str = "안녕하세요 홍길동님";

 

                    //안녕으로 시작 합니까?

                    Console.WriteLine(str.StartsWith("안녕"));

                    Console.WriteLine(str.StartsWith("반갑"));

                    Console.WriteLine(str.EndsWith(""));

                    Console.WriteLine(str.EndsWith("입니다."));

             }

 


private static void M5()

             {

                    //게시판 글쓰기 -> 금지어

                    string data = "게시판이 어쩌구 바보 저쩌구... `~~~";

 

                    //Console.WriteLine(data.Contains("바보"));

                    //금지어가 여러개일 경우

                    string[] words = {"바보", "멍청이", "메롱"};

 

                   //true가 들어 있을경우 글을써도 된다 false일경우 금지어이기 떄문에 안된다.

                    bool flag = true;

 

                    for (int i = 0; i < words.Length; i++)

                    {

                           //금지어 갯수만큼 회전

                           //Console.WriteLine(data.Contains(words[i]));

 

                           if (data.Contains(words[i]))

                           {

                                 flag = false;

                                 break;

                           }

                    }

 

                    if (flag)

                    {

                           Console.WriteLine("글쓰기 작업진행..");

                    }

 

                    else

                    {

                           Console.WriteLine("금지어 발견!!");

                    }

             }

 


       private static void M4()

             {

                    //Contain() : 포함유무 확인 => 일종의 검색

                    string str = "안녕하세요 홍길동님";

 

                    Console.WriteLine(str.Contains("홍길동"));

                    Console.WriteLine(str.Contains("이순신"));

 

                    //STR안에 공백이?

                    Console.WriteLine(str.Contains(" "));

             }

 


private static void M3()

             {

                    //이름을 입력(2-5)

                    Console.Write("이름 입력 : ");

                    string name = Console.ReadLine();

 

                    if (name.Length >= 2 && name.Length <=5)

                    {

                           Console.WriteLine("통과");

                    }

                    else

                    {

                           Console.WriteLine("2~5자 이내..");

                    }

             }

 


private static void M2()

             {

                    //문자열 길이(몇글자?)

                    string str = "안녕하세요 아이유님";

                    Console.WriteLine(str.Length);

             }

 


public static void M1()

             {

                    //C# 예약어               

                    //int n1 = 10;

 

                    //CTS 표현(닷넷언어 공통)

                    //System.Int32 n2 = 20;

 

                    //C# 예약어

                    //string str1 = "홍길동";

 

                    //System.이 생략 되있는 문구

                    //System.String str2 = "아무게";

 

                    //값형? 참조형? 구별하는 방법!! => 크기!!

                    //int :

                    //byte :

                    //char :

                    //struct :

                    //int[] : 참조형

                    //string : 참조형

 

                    //공간 하나당 4byte

                    //값형의 의미 : 정해져 있는 공간 byte 4

                    //메모리 할당 크기가 정해져 있는것을 값형이라하고

                    //메모리 할당 크기가 정해져 있지 않는 것을 참조 타입이라고 한다.

 

                    string name1 = "이순신";

                    char[] name2 = new char[3] { '', '', '' };

 

                    //Console.WriteLine(name2[0]);

                    //Console.WriteLine(name1[0]);

 

                    //string char[]처럼 사용 => 인덱서(Indexer)

                    //스트링을 배열 처럼 사용시에는 읽기전용 으로만 된다.

                    for (int i = 0; i < 3; i++)

                    {

                           Console.WriteLine(name1[i]);

                    }

             }

       }

}

 

 

반응형