- String.Compare(s1, s2, true|false|null), s1.CompareTo(s1, s2), s1.Equals(s2)

s2>s1 return 1

s1>s2 return -1

s1 == s2 문자의 길이가 같으면, 첫번째 문자의 크기를비교(알파벳상 뒤면 더크다)

true일때는 대소문자 무시(스페이스로두어도됨), 대소문자 구분은 false (a > A)

using System;
					
public class Program
{
	public static void Main()
	{
		string str1 = "Korea";
		string str2 = "Canada";
		string str3 = "Corea";
		string str4 = "korea";
		
		Console.WriteLine("{0} {1}", str1.Length, str2.Length);
		var res1 = String.Compare(str1, str2);  // str2 > str1 return 1
		Console.WriteLine(res1);
		var res2 = String.Compare(str2, str1);  // str2 < str1 return -1
		Console.WriteLine(res2);
		var res3 = String.Compare(str1, str3);  // str1 == str3 and C>K return 1
		Console.WriteLine(res3);
		var res4 = String.Compare(str1, str4, false);  // str1 == str4 and K==K return 0
		Console.WriteLine(res4);
		
		// CompareTo()
		var res5 = str1.CompareTo(str2);
		Console.WriteLine(res5);
		
		// Compare two strings whether same or not: return true | false
		Console.WriteLine(str1.Equals(str2));
	}
}

- ToCharArray(), Split(''), Join(",", arr)

  : ToCharArray(): string을 char array로 변환

  : Split(''): string을 셀렉터로 구분하여 배열로 저장

  : Join('', arr): arr을 셀렉터로 구분하여 스트링으로 저장

using System;
					
public class Program
{
	public static void Main()
	{
		string str1 = "Korea";
		string str2 = “Korea,Japan,China,Canada”;
		
        char[] arr = str1.ToCharArray();
		Console.WriteLine(arr[2]);
		
        string[] arr2 = str1.Split(',');
		Console.WriteLine(arr2[2]);
        
        string str3 = String.Join("-", arr2);
		Console.WriteLine(str3);
	}
}
/* r China Korea-Japan-China-Canada */

- Contains(), IndexOf(), LastIndexOf()

 : Contains(): 문자(열) 포함을 확인 또는 문자열비교  --  return true | false

 : IndexOf(): 문자(열)의 처음나오는 위치를 리턴, 존재하지 않는경우 -1

 : LastIndexOf(): 문자(열)의 마지막 나오는 위치를 리턴 

using System;
					
public class Program
{
	public static void Main()
	{
		string str1 = "Be the reds";
		string str2 = "Be the reds";
		string str3 = "be the reds";
		
		// "the"가 있는지 체크 return true
		Console.WriteLine(str1.Contains("the"));
		
		// "The"가 있는지 체크 return false  --  대소문자 구분
		Console.WriteLine(str1.Contains("The"));
		
		// 두개의 스트링 비교 return true
		Console.WriteLine(str1.Contains(str2));
		
		// 두개의 스트링 비교 return false  -- 대소문자 구분
		Console.WriteLine(str1.Contains(str3));
		
		// 문자 "r"의 위치 출력 return 7, 문자열 검색시 맨첫문자의 위치
		Console.WriteLine(str1.IndexOf("r"));
		
		// 문자 "k"의 위치 출력 return -1  --  존재하지 않음
		Console.WriteLine(str1.IndexOf("k"));
        
        // 문자 "r"의 위치 출력 return 8, 문자열 검색시 해당 문자가 마지막으로 나오는 위치
		// 문자열일 경우 마지막에 나오는 위치중 맨첫문자의 위치
		Console.WriteLine(str1.LastIndexOf("e"));
	}
}
/* True  False  True  False  7  -1  -1  8  */

- Substring(n, num), Insert(n, str), Remove(n, num), Replace(n, str)

 : Substring(): n번째에서 num개 만큼 선택

 : Insert(): n번째에 특정 스트링 입력

 : Remove(): n번째에서 num개 만큼 제거  -- Substring과 Insert의 반대로 볼수도 있다.

 : Replace(): n번째에서 특정 스트링 교환

using System;
					
public class Program
{
	public static void Main()
	{
		string str1 = "Korea, Canada, Japan, China, England";
		
		// 3번째에서 5개 return ea, C
		Console.WriteLine("Substring 1: " + str1.Substring(3,5));
		
		// 7번째에서 모두다 Canada, Japan, China, England
		Console.WriteLine("Substring 2: " + str1.Substring(7));
		
		// 0번째에서 5개  -- ,가 5번째 위치함
		Console.WriteLine("Substring 3: " + str1.Substring(0, str1.IndexOf(',')));
		
		// 6번째에 " Russia," 삽입
		Console.WriteLine("Insert: " + str1.Insert(6, " Russia,"));
		
		// 6번째에서 8개의 캐릭터 제거
		Console.WriteLine("Remove 1: " + str1.Remove(6,8));
		
		// 5번째이후 모두 제거
		Console.WriteLine("Remove 2: " + str1.Remove(5));
		
		// , 를 | 로 변환
		Console.WriteLine("Replace: " + str1.Replace("," , "|"));
	}
}
/*
 Substring 1: ea, C
 Substring 2: Canada, Japan, China, England
 Substring 3: Korea
 Insert: Korea, Russia, Canada, Japan, China, England
 Remove 1: Korea, Japan, China, England
 Remove 2: Korea
 Replace: Korea| Canada| Japan| China| England
*/

- Trim(), TrimStart(), TrimEnd()

 : Trim() 앞뒤공백제거, Trim(‘*’, ‘a’) 앞뒤로 제거할 캐릭터 지정가능

 : TrimStart() 앞쪽 공백제거

 : TrimEnd() 뒷쪽 공백제거

using System;
					
public class Program
{
	public static void Main()
	{
		string str1 = " hello there ";
		string str2 = "w**hello..";
		string str3 = "hhhhello***";
		// Remove whitespaces from both sides
		Console.WriteLine(str1.Trim());
		
		// Remove special characters 앞뒤 다르게
		Console.WriteLine(str2.Trim('w','.'));
		
		// Remove special characters 앞뒤 같게, 같은 캐릭터는 다 없애기
		Console.WriteLine(str3.Trim('h'));
		
		// Remove whitespace from start
		Console.WriteLine(str1.TrimStart());
		
		// Remove whitespace from end
		Console.WriteLine(str1.TrimEnd());
	}
}
/*
 hello there
 **hello
 ello***
 hello there 
  hello there
*/

- ToLower(), ToUpper(), StartsWith(), EndsWith(), GetType(), Sort()

- string.Empty, Length, HasValue

- Convert.ToInt32(str), int.Parse(str)

'ASP.NET C#' 카테고리의 다른 글

Generic(제너릭)  (0) 2021.01.27
yield, paraller, dynamic  (0) 2021.01.21
Delegate, Action, Func  (0) 2021.01.19
Members => Property(프로퍼티)  (0) 2021.01.16
enum(열거형)  (0) 2021.01.15

공통된 코드를 사용하고, 타입만 달라질 경우 사용

1. 일반적인 클래스 사용 (int, string)

// int id, string name
using System;

namespace Example
{
	public class Student
	{
		public int id {get; set;}
		public string name {get; set;}
		public Student(int id, string name)
		{
			this.id = id;
			this.name = name;
		}
		public int getId()
		{
			return this.id;
		}
		public string getName()
		{
			return this.name;
		}		
	}
    		
	public class Program
	{				
		// execute
		public static void Main(string[] args)
		{
			Student st = new Student(10, "John");
			Console.WriteLine(st.getName() + "'s ID is " + st.getId());
		}
	}
}

2. 일반적인 코드 사용(string id, string name) 그런데 타입만 달라진다.

// string id, string name
using System;

namespace Example
{
	public class Student
	{
		public string id {get; set;}
		public string name {get; set;}
		public Student(string id, string name)
		{
			this.id = id;
			this.name = name;
		}
		public string getId()
		{
			return this.id;
		}
		public string getName()
		{
			return this.name;
		}		
	}
    		
	public class Program
	{				
		// execute
		public static void Main(string[] args)
		{
			Student st = new Student("ten", "John");
			Console.WriteLine(st.getName() + "'s ID is " + st.getId());
		}
	}
}	

3. 코드의 낭비를 없애기 위해 제너릭 사용

  : 클래스 선언시 이름 옆에 <T>, 타입이 들어가는 자리에 T 표기

  : 사용시에는 클래스 이름옆에 <타입> 넣어준다

// 제네릭 T : 한개의 변수 사용예정, string id, string name
using System;

namespace Example
{
	public class Student<T>
	{
		public T id {get; set;}
		public T name {get; set;}
		public Student(T id, T name)
		{
			this.id = id;
			this.name = name;
		}
		public T getId()
		{
			return this.id;
		}
		public T getName()
		{
			return this.name;
		}		
	}
    		
	public class Program
	{				
		// execute
		public static void Main(string[] args)
		{
			Student<string> st = new Student<string>("ten", "John");
			Console.WriteLine(st.getName() + "'s ID is " + st.getId());
		}
	}
}	

4. 제네릭 2개의 변수 사용

// 제네릭 T,S 사용
using System;

namespace Example
{
	public class Student<T, S>
	{
		public T id {get; set;}
		public S name {get; set;}
		public Student(T id, S name)
		{
			this.id = id;
			this.name = name;
		}
		public T getId()
		{
			return this.id;
		}
		public S getName()
		{
			return this.name;
		}		
	}
    		
	public class Program
	{				
		// execute
		public static void Main(string[] args)
		{
			Student<int, string> st = new Student<int, string>(10, "John");
			Console.WriteLine(st.getName() + "'s ID is " + st.getId());
		}
	}
}	

'ASP.NET C#' 카테고리의 다른 글

String Manipulation  (0) 2021.01.29
yield, paraller, dynamic  (0) 2021.01.21
Delegate, Action, Func  (0) 2021.01.19
Members => Property(프로퍼티)  (0) 2021.01.16
enum(열거형)  (0) 2021.01.15

yield 키워드

iteration 해주는 키워드

using System;
using System.Collections.Generic;

namespace Example
{   	
	public class Program
	{
		static IEnumerable<int> GetNumber()
		{
			yield return 100;
			yield return 200;
			yield return 300;
		}
				
		// execute
		public static void Main(string[] args)
		{
			foreach(int num in GetNumber())
			{
				Console.WriteLine(num);
			}
		}
	}
}	

 

paraller 키워드

여러개의 cpu 병렬처리 하게끔 도와주는 키워드

Paraller.For(0, 100, (i) => {Console.WriteLine(i + “ “);}

 

dynamic 키워드

컴파일러가 런타임때까지 타입체크를 하지 않는다. dynamic 타입은 object타입을 사용하며, 캐스팅이 필요없다

 

'ASP.NET C#' 카테고리의 다른 글

String Manipulation  (0) 2021.01.29
Generic(제너릭)  (0) 2021.01.27
Delegate, Action, Func  (0) 2021.01.19
Members => Property(프로퍼티)  (0) 2021.01.16
enum(열거형)  (0) 2021.01.15

사전적 정의로 attribute는 특성, preperty는 자산,특질

attribute: 정적이며, html문서에 존재

property: 동적으로, Dom tree에 존재 (Dom Tree 에서 attribute에 대한 표현)

 

<p class="myClass" id="name">Jeo</p>

<p></p>: Tag

class, id: attribute

myClass, name: attribute의 value

Jeo: Content

전체: element

 

이를 Dom Tree에서 확인하면 값이 myClass인 className이라는 property가 존재한다.

예를들어, 체크박스에 체크를해서 값을 변경할때, attribute의 값은 변하지 않지만,

attribute는 동적으로 변화한다.

'HTML' 카테고리의 다른 글

Web Storage  (0) 2020.12.31
HTML Input Form  (0) 2020.12.30

+ Recent posts