Shifting a String in C#
how about this?
public static string ShiftString(string t)
{
return t.Substring(1, t.Length - 1) + t.Substring(0, 1);
}
You can try this:
s = s.Remove(0, 1) + s.Substring(0, 1);
As an extension method:
public static class MyExtensions
{
public static string Shift(this string s, int count)
{
return s.Remove(0, count) + s.Substring(0, count);
}
}
Then you can use:
s = s.Shift(1);
The algorithm to solve this type of problem about shift n positions is duplicate the string, concatenate together and get the substring. ( n < length(string) )
string s = "ABCDEFGH";
string ss = s + s; // "ABCDEFGHABCDEFGH"
if you want to shift n position, you can do
var result = ss.Substring(n, s.length);