자주 쓰는 메서드
문자열은 string 으로 큰 따옴표(""), 문자는 char('') 로 작은 따옴표로 표기된다
공백추가 | PadLeft(), PadRight() |
공백제거 | Trim(), TrimStart(), TrimEnd() |
문자열 길이 갯수 | Length 속성 |
string을 char로 분리 | Split() |
문자열에 필요한 부분을 추출 | Substring() |
시작하는 문자열 찾기 | StartsWith() |
마지막 문자열 찾기 | EndsWith() |
포함된 문자열 찾기 | Contains() |
문자열을 문자 배열로 변경 | ToCharArray() |
내용변경 | Replace() |
내용추가 | Insert() |
내용삭제 | Remove() |
서식 예시
string customerName = "홍길동";
string currentProduct = "진행중작업";
int currentShares = 2975000;
decimal currentReturn = 0.1275m;
decimal currentProfit = 55000000.0m;
string newProduct = "새로운작업";
decimal newReturn = 0.13125m;
decimal newProfit = 63000000.0m;
Console.WriteLine($"{customerName}님께 {currentProduct}에 대해 보고드립니다 \n");
Console.WriteLine($"투자금 {currentShares:N}원에 대한\n진행중인 작업의 수익률은 {currentReturn:P}이고,");
Console.WriteLine($"{newProduct}의 수익률은 {newReturn:P}입니다 {newProfit:C}.\n");
string comparisonMessage = "";
comparisonMessage = currentProduct.PadRight(10);
comparisonMessage += String.Format("{0:P}", currentReturn).PadRight(10);
comparisonMessage += String.Format("{0:C}", currentProfit).PadLeft(10);
comparisonMessage += "\n";
comparisonMessage += newProduct.PadRight(10);
comparisonMessage += String.Format("{0:P}", newReturn).PadRight(10);
comparisonMessage += String.Format("{0:C}", newProfit).PadLeft(10);
Console.WriteLine(comparisonMessage);
output :
홍길동님께 진행중작업에 대해 보고드립니다
투자금 2,975,000.00원에 대한
진행중인 작업의 수익률은 12.75 %이고,
새로운작업의 수익률은 13.13 %입니다 ¤63,000,000.00.
진행중작업 12.75 % ¤55,000,000.00
새로운작업 13.13 % ¤63,000,000.00
복합 서식 지정 - string.Format({}, ...)
string str1 = "hello";
string str2 = "world";
string result = string.Format("{0} {1}!", str1, str2);
output :
hello world!
tip. 서식 관련 추가적인 내용을 확인할 수 있는 링크
표준 숫자 서식 문자열 링크
https://docs.microsoft.com/ko-kr/dotnet/standard/base-types/standard-numeric-format-strings
String.Format 메서드
https://docs.microsoft.com/ko-kr/dotnet/api/system.string.format?view=net-5.0
'공방 > 학습 일지' 카테고리의 다른 글
Visual Studio 2019 언어를 영문으로 설정 하기 (4) | 2020.12.17 |
---|---|
[ .NET ] 파싱한 문자열에서 데이터 추출하기 (0) | 2020.12.07 |
[ .NET ] c#에서 데이터 변환 및 정렬 활용하기 (0) | 2020.11.23 |
[ Python ] 을 시작해보자 (0) | 2020.11.20 |
[ .NET ] File 및 Directory 작업 활용2 (0) | 2020.11.16 |