char[] charArr = { 'A', 'B', 'C', '\0'}; //문자열 배열
Console.WriteLine(charArr);
//Console.WriteLine은 여러 변수들을 받아 알맞게 출력하는 함수들을 오버로딩 해놓았다
string strTest = new string("strTest!"); // string : 마우스 커서 올려보면 calss 확인 가능
string strChar = new string(charArr);
string strConst = "strConst";
char와 string의 데이터 타입이 다른 것을 확인 할 수 있다.
static void Main()
{
// 문자열(String)
char[] charArr = { 'A', 'B', 'C', '\0'};
Console.WriteLine(charArr);
string strTest = new string("strTest!"); // string : 마우스 커서 올려보면 calss 확인 가능
string strChar = new string(charArr);
string strConst = "strConst";
//Console.WriteLine - 여러가지 오버로딩이 되어 있어서 많은 형식을 출력 할 수 있다
Console.WriteLine("ToUpper: " + strTest.ToUpper()); //모두 대문자
Console.WriteLine("ToLower: " + strTest.ToLower()); //모두 소문자
string insert = strTest.Insert(3, "qwer"); //문자 삽입
Console.WriteLine("Insert: " + insert);
Console.WriteLine("strTest Length: " + strTest.Length); //문자열 길이
string replace = strTest.Replace("Test", "Example"); //
Console.WriteLine("Replace: " + replace);
string exam = " Example ";
Console.WriteLine("exam: " + exam);
string trim = exam.Trim(); //문자열 제거 - default : 제일 끝에 있는 띄어쓰기 제거
Console.WriteLine("trim: " + trim);
char[] trimChars = { 'l', 'e', 'E', 'm' };
Console.WriteLine("trim with chars: " + trim.Trim(trimChars));
//제일 끝에 있는 문자를 제거함, 중간의 글자는 x, 연속될 경우 해당 문자까지 삭제
Console.WriteLine("INdexOf: " + strTest.IndexOf('r')); //해당 문자의 인덱스 위치
Console.WriteLine("Contains: " + strTest.Contains("rTe")); //해당 문자열이 있는지 검사
Console.WriteLine("Remove: " + strTest.Remove(1, 3));//문자열에서 해당 인덱스 삭제
string splitTest = "kim, lee, pack, choi";
string[] splitList = splitTest.Split(','); // , 을 기준으로 배열에 배치
foreach (string str in splitList) //배열 출력 확인용 foreach
Console.WriteLine(str);
Console.WriteLine();
Console.WriteLine("String" + 1 + "Test" + 3.14F);
Console.WriteLine("Size: " + sizeof(int) + sizeof(float));
Console.WriteLine(sizeof(int) + sizeof(float)+ " bytes");
Console.WriteLine은 출력 순서에 따라 식이 달라짐(함수 오버로딩 순서에 따라 결정)
int val = 10;
Console.WriteLine(val.GetType());
Console.WriteLine(val.ToString().GetType());
Console.WriteLine("Compare: " + (strTest == strChar));
//문자열을 비교해서 같으면 true -> 연산자 오버로딩이 되어 있다
string newStr1 = new string("New");
string newStr2 = new string("New");
Console.WriteLine("newStr1 == newStr2: " + (newStr1 == newStr2));
Console.WriteLine("newStr1.CompareTo(newStr2): " + newStr1.CompareTo(newStr2));//위에보다 얘가 속도가 빠름
//C#의 모든 자료형은 object를 상속받고 있다.
Console.WriteLine("object.ReferenceEquars: " + object.ReferenceEquals(newStr1, newStr2));
//같은 주소를 참조하고 있는지에 대한 판별
Console.WriteLine("newStr1.Equals: " + newStr1.Equals(newStr2));
string constStr1 = "Const"; //리터널상수 - C#에서는 이미 사용한 상수를 그대로 재활용 한다. - 메모리를 더 쓰는 일은 없다, 다만 찾는데 시간이 조금 더 걸린다.
string constStr2 = "Const";
Console.WriteLine("constStr1 == constStr2: " + (constStr1 == constStr2));
Console.WriteLine("constStr1.CompareTo(constStr2): "+ constStr1.CompareTo(constStr2));
Console.WriteLine("object.ReferenceEquals: " + object.ReferenceEquals(constStr1, constStr2));
Console.WriteLine("constStr1.equals: "+constStr1.Equals(constStr2));
ReferenceEquals는 같은 주소를 참조하고 있는지에 대한 확인을 진행한다.
string alphabat = "A" + "B" + "C" + "D"; // 이런 방식은 효율적이지 못함
StringBuilder sb = new StringBuilder();
sb.Append("This ");
sb.Append("is ");
sb.Append("String ");
sb.Append("Builder");
Console.WriteLine(sb.ToString());
char[] chars = alphabat.ToCharArray();
문자열의 추가는 Append를 활용하면 메모리를 효율적으로 활용 할 수 있다.
'프로그래밍 언어 > C#' 카테고리의 다른 글
생성자(Consturctor)와 소멸자(Destructor) (0) | 2024.09.05 |
---|---|
객체 지향 프로그래밍(Object Oriented Programming) (2) | 2024.09.04 |
Garbage Collection(가비지 컬렉션) (3) | 2024.09.04 |
Namespace (0) | 2024.08.30 |
구조체(Structure) / 함수 오버로딩(Function Overloading) (0) | 2024.08.29 |