C#中的字符串切片以旋转字符串
假设我们的字符串是-
var str = "welcome";
substring()
如果您只想旋转某些字符,请使用以下方法。在这里,我们只旋转2个字符-
var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2);
以下是完整的代码-
示例
using System; public class Program { public static void Main() { var str = "welcome"; Console.WriteLine("Original String = "+str); var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2); Console.WriteLine("Rotating two characters in the String: "+res); } }
输出结果
Original String = welcome Rotating two characters in the String: elcomewe