סקור את הפתרון כדי לחלץ, להחליף ולהסיר נתונים ממחרוזת קלט

הושלמה

הקוד הבא הוא פתרון אפשרי אחד לאתגר ביחידה הקודמת.

const string input = "<div><h2>Widgets &trade;</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 = "&trade;";
const string regSymbol = "&reg;";
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 &reg;</h2><span>5000</span>

אם תושלם בהצלחה, מזל טוב! המשך לשלב בדיקת הידע ביחידה הבאה.

חשוב

אם אתה נתקל בבעיות בהשלמת אתגר זה, ייתכן שעליך לסקור את היחידות הקודמות לפני שתמשיך.