Substring - 일부분 가져오기 |
string s3 = "Visual C# Express"; System.Console.WriteLine(s3.Substring(7, 2)); // Output: "C#"
|
String[] pairs = { "Color1=red", "Color2=green", "Color3=blue", "Title=Code Repository" }; foreach (var pair in pairs) { int position = pair.IndexOf("="); if (position < 0) continue; Console.WriteLine("Key: {0}, Value: '{1}'", pair.Substring(0, position), pair.Substring(position + 1)); }
// The example displays the following output: // Key: Color1, Value: 'red' // Key: Color2, Value: 'green' // Key: Color3, Value: 'blue' // Key: Title, Value: 'Code Repository'
|
문자열 바꾸기
|
System.Console.WriteLine(s3.Replace("C#", "Basic")); // Output: "Visual Basic Express"
|
string source = "The mountains are behind the clouds today.";
// Replace one substring with another with String.Replace. // Only exact matches are supported. var replacement = source.Replace("mountains", "peaks"); Console.WriteLine($"The source string is <{source}>"); Console.WriteLine($"The updated string is <{replacement}>"); |
문자열 있는 인덱스 가져오기 |
// Index values are zero-based int index = s3.IndexOf("C"); // index = 7 |
문자열 분리 |
string phrase = "The quick brown fox jumps over the lazy dog."; string[] words = phrase.Split(' '); foreach (var word in words) { System.Console.WriteLine($"<{word}>"); } |