문자열, char와 서식 지정
Coddy의 Go 개발자를 위한 C# 코스 레슨. 48개 중 5번째.
Go 문자열에는 바이트가 포함되고 인덱싱하면 바이트가 반환되는 반면, C# 문자열은 UTF-16 char 단위를 노출합니다. Go의 rune은 유니코드 코드 포인트이며, 모든 문자에 대해 C# char와 동등하지는 않습니다. ASCII 테스트 데이터는 이 연습의 초점을 형식 지정과 정규화에 맞춰 줍니다.
문자열은 변경할 수 없습니다. Trim과 ToUpperInvariant는 원본을 수정하는 대신 새 문자열을 반환합니다. char에는 문자를 나타낼 때 작은따옴표를 사용하고, string에는 큰따옴표를 사용합니다. 보간 문자열에는 달러 기호 접두사와 중괄호를 사용합니다. 여러 컴퓨터에서 출력이 일관되어야 한다면 고정 로캘 대소문자 변환과 형식 지정을 사용하세요.
Go 참고 코드
package main
import ("fmt"; "strings")
func main() {
name := " pen "; fmt.Printf("%s:%d\n",strings.ToUpper(strings.TrimSpace(name)),2)
}Main 내부의 C# 문
string name = " pen ";
Console.WriteLine($"{name.Trim().ToUpperInvariant()}:{2}");이 내용은 특정 개념에 초점을 맞춘 교육용 발췌문입니다. 과제 파일에는 완전한 실행 가능 래퍼가 포함되어 있으며, 위 스니펫은 관련 선언과 문을 보여 줍니다.
참조 출력:
PEN:2C# 출력:
PEN:2챌린지
쉬움입력은 ASCII 이름이 있는 한 줄과 정수 개수가 있는 한 줄입니다. 앞뒤 공백을 제거하고 대문자로 변환한 이름, 콜론, 개수를 반환하세요. 중간 공백은 유지하세요.
잠금 해제된 C# 파일을 수정하세요. 잠긴 상태로 표시된 Main.cs는 설명된 줄을 읽고, Exercise.Solve를 호출하고, 입력이 변경되지 않았는지 확인한 다음 결과를 마지막 줄바꿈 하나와 함께 출력합니다. 선언된 API를 유지하세요. 두 번째 Main을 추가하거나 진단 출력을 출력하지 마세요.
직접 해보기
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = input[0]; string before0=Snapshot(a0);
var a1 = I(input[1]); string before1=Snapshot(a1);
var result = Exercise.Solve(a0,a1);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
Console.WriteLine(result);
}
}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
Go 개발자를 위한 C#의 모든 레슨
1여기서 시작하기
첫 번째 C# 프로그램 실행하기직접 연습해 보세요: 온라인 C# 컴파일러