솔루션을 검토하여 입력 문자열에서 데이터를 추출, 바꾸기 및 제거
다음 코드는 앞 단원의 과제에 대한 가능한 한 가지 솔루션입니다.
const string input = "<div><h2>Widgets ™</h2><span>5000</span></div>";
string quantity = "";
string output = "";
// Your work here
// Extract the "quantity"
const string openSpan = "<span>";
const string closeSpan = "</span>";
int quantityStart = input.IndexOf(openSpan) + openSpan.Length; // + length of <span> so index at end of <span> tag
int quantityEnd= input.IndexOf(closeSpan);
int quantityLength = quantityEnd - quantityStart;
quantity = input.Substring(quantityStart, quantityLength);
quantity = $"Quantity: {quantity}";
// Set output to input, replacing the trademark symbol with the registered trademark symbol
const string tradeSymbol = "™";
const string regSymbol = "®";
output = input.Replace(tradeSymbol, regSymbol);
// Remove the opening <div> tag
const string openDiv = "<div>";
int divStart = output.IndexOf(openDiv);
output = output.Remove(divStart, openDiv.Length);
// Remove the closing </div> tag and add "Output:" to the beginning
const string closeDiv = "</div>";
int divCloseStart = output.IndexOf(closeDiv);
output = "Output: " + output.Remove(divCloseStart, closeDiv.Length);
Console.WriteLine(quantity);
Console.WriteLine(output);
이 코드는 단지 "하나의 가능한 솔루션"에 불과합니다. 코드에서 다음 출력을 생성하는 한 성공했습니다.
Quantity: 5000
Output: <h2>Widgets ®</h2><span>5000</span>
성공하면 축하합니다! 다음 단원에서 지식 점검을 계속 진행합니다.
중요합니다
이 과제를 완료하는 데 문제가 있는 경우 계속 진행하기 전에 이전 단원을 복습해야 할 수도 있습니다.