我在做练习,它也提供了解决方案,但是没有解释解决方案中的代码,并且无法理解代码。希望我能理解它替换两个字符串的第一个字符和最后一个字符的任务
练习得到的帮助: 写C#程序从给定的字符串,其中第一个和最后一个字符会改变它们的位置创建一个新的字符串。
字符串: w3resource 的Python
预期输出: e3resourcw nythoP
解决方案:
public class Exercise16 {
static void Main(string[] args)
{
Console.WriteLine(first_last("w3resource"));
Console.WriteLine(first_last("Python"));
Console.WriteLine(first_last("x"));
}
public static string first_last(string ustr)
{
// code that I don't understand
return ustr.Length > 1
? ustr.Substring(ustr.Length - 1) + ustr.Substring(1, ustr.Length - 2) + ustr.Substring(0, 1) : ustr;
}
}
PS - 我在C#中,但不是在初学者亲整体拼图
啊我明白了。感谢您一行一行地解释微型'if'语句。很有用 – Zane