ตรวจทานโซลูชันเพื่อแยก แทนที่ และเอาข้อมูลจากสตริงที่ป้อนเข้าออก
โค้ดต่อไปนี้เป็นหนึ่งในวิธีแก้ไขปัญหาที่เป็นไปได้สําหรับการทดสอบจากหน่วยก่อนหน้า
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>
ถ้าสําเร็จ ขอแสดงความยินดี! ดําเนินการต่อไปที่การตรวจสอบความรู้ในหน่วยถัดไป
สําคัญ
หากคุณประสบปัญหาในการดําเนินการการทดสอบนี้ให้เสร็จสมบูรณ์ บางทีคุณควรตรวจสอบหน่วยก่อนหน้านี้ก่อนที่คุณจะดําเนินการต่อ