본문 바로가기
C++ 200제/코딩 IT 정보

C# string 예제 Compare, Contains, Substring, Join 4가지

by vicddory 2017. 8. 25.

C# string 예제 Compare, Contains, Substring Join 4가지


C# String 키워드는 System.String 클래스에서 존재합니다.


string을 사용하는 예제 하나 살펴보겠습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
namespace StringApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         // string literal
         string fname, lname;
         fname = "Rowan";
         lname = "Atkinson";
         
         string fullname = fname + lname;
         Console.WriteLine("Full Name: {0}", fullname);
         
         // string 생성자 사용
         char[] letters = { 'H''e''l''l','o' };
         string greetings = new string(letters);
         Console.WriteLine("Greetings: {0}", greetings);
         
         // string return 함수
         string[] sarray = { "Hello""From""Tutorials""Point" };
         string message = String.Join(" ", sarray);
         Console.WriteLine("Message: {0}", message);
         
         // string 포맷팅 함수와 변환 
         DateTime waiting = new DateTime(2012101017581);
         string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
         Console.WriteLine("Message: {0}", chat);
      }
   }
}
cs


출력


1
2
3
4
Full Name: Rowan Atkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, October 102012

cs

1. string 비교


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string str1 = "This is test";
         string str2 = "This is text";
 
         if (String.Compare(str1, str2) == 0)
         {
            Console.WriteLine(str1 + " and " + str2 +  " are equal.");
         }
         else
         {
            Console.WriteLine(str1 + " and " + str2 + " are not equal.");
         }
         Console.ReadKey() ;
      }
   }
}
 
출력 :
This is test and This is text are not equal.
cs


2. Contains() 문자열 검색


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string str = "This is test";
         if (str.Contains("test"))
         {
            Console.WriteLine("The sequence 'test' was found.");
         }
         Console.ReadKey() ;
      }
   }
}
 
출력:
The sequence 'test' was found.
cs


C# string 예제 Compare, Contains, Substring, Join 4가지[C# string 예제 Compare, Contains, Substring, Join 4가지]


3. Substring() 문자열 자르기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string str = "Last night I dreamt of San Pedro";
         Console.WriteLine(str);
         string substr = str.Substring(23);
         Console.WriteLine(substr);
      }
   }
}
 
출력:
San Pedro
cs


4. Join() 문자열 추가


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string[] starray = new string[]{"Down the way nights are dark",
         "And the sun shines daily on the mountain top",
         "I took a trip on a sailing ship",
         "And when I reached Jamaica",
         "I made a stop"};
 
         string str = String.Join("\n", starray);
         Console.WriteLine(str);
      }
   }
}
 
출력:
Down the way nights are dark
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop
cs


C# string 예제 Compare, Contains, Substring Join 4가지

댓글