2013-02-24 42 views
8
static void Main(string[] args) 
    { 
    string s = "ABCDEFGH"; 
    string newS = ShiftString(s); 
    Console.WriteLine(newS); 
    } 
    public static string ShiftString(string t) 
    { 
    char[] c = t.ToCharArray(); 
    char save = c[0]; 
    for (int i = 0; i < c.Length; i++) 
    { 
     if (c[i] != c[0]) 
     c[i] = c[i - 1]; 
    } 
    Console.WriteLine(c); 
    String s = new string(c); 
    return s; 
    } 

我需要字符串s一个空间转移到左边,所以我最终的字符串:“BCDEFGHA” 所以我想过改变字符串转换成字符数组和工作,我的从那里的方式,但我不知道如何成功做出这项工作。我很确定我需要一个for循环,但是我需要一些帮助,以便如何将char序列向左移动一个空格。转移在C#中的字符串

+0

结果如何看起来像? – spajce 2013-02-24 15:57:22

+0

你说你最终得到了“BCDEFGHA”。这不是你所期望的吗?当输入“ABCDEFGH”时,你期望得到什么结果? – zsong 2013-02-24 16:08:10

+0

对不起,我可能不正确地使用它。我期待结果“BCDEFGHA”,但这不是我目前的输出结果。 – user2104751 2013-02-24 16:18:55

回答

8

这个怎么样?

public static string ShiftString(string t) 
{ 
    return t.Substring(1, t.Length - 1) + t.Substring(0, 1); 
} 
+0

谢谢,这似乎工作,但我不是100%确定这实际上是如何工作的。会善意解释它为什么这样做? – user2104751 2013-02-24 16:21:28

+0

想象一下你有这个字符串'ABC'。这部分't.Substring(1,t。长度 - 1)''会产生'BC',而这个't.Substring(0,1)'将返回'A'。 – 2013-02-24 16:23:07

+2

哦,等等,我可能知道。 t.Substring(1,t.Length - 1)翻译为“BCDEFGH”,t.Substring(0,1)翻译为“A”,并将这些连接起来得到“BCDEFGHA”? – user2104751 2013-02-24 16:24:45

7

你可以试试这个:

s = s.Remove(0, 1) + s.Substring(0, 1); 

作为一个扩展方法:

public static class MyExtensions 
{ 
    public static string Shift(this string s, int count) 
    { 
     return s.Remove(0, count) + s.Substring(0, count); 
    } 
} 

然后你可以使用:

s = s.Shift(1); 
+0

+1使它成为一个扩展名:) – 2014-04-07 14:43:40

3

个人而言,我应该这样做:

public static string ShiftString(string t){ 
    string firstLetter = t.Substring(0, 1); 

    return t.Substring(1) + firstLetter; 
} 
+0

对不起,你不能为你+1,因为这个答案已被使用。 – 2014-04-07 14:42:53

2

你可以采取的事实stringIEnumerable<char>优势:

public static string ShiftString(string t){ 
    return new String(t.Skip(1).Concat(t).Take(t.Length).ToArray()); 
} 
1

StringBuilder类为您提供更好的性能

static string ShiftString(string str) 
{ 
    if (str == null) throw new ArgumentNullException(); 
    int strLen = str.Length; 
    if (strLen == 0) return string.Empty; 
    StringBuilder sb = new StringBuilder(strLen); 
    sb.Append(str, 1, strLen - 1); 
    sb.Append(str[0]); 
    return sb.ToString(); 
} 
5

解决此类问题的有关转移算法n位置复制字符串,连接在一起得到子字符串。 (ñ<长度(串)

string s = "ABCDEFGH"; 
string ss = s + s; // "ABCDEFGHABCDEFGH" 
如果你想转移 ň位置

,你可以做

var result = ss.Substring(n, s.length); 
+0

+1感谢这种方法非常有帮助 – 2014-04-07 14:41:58

1

以下方法采取数n,它告诉你要多少次移动/旋转字符串。如果数字大于字符串的长度,我已将MOD按字符串长度取出。

public static void Rotate(ref string str, int n) 
    { 
     //if the rotation/shift number is less than or =0 throw exception 
     if (n < 1) 
      throw new Exception("Negative number for rotation"); 
     //if the String is less than 1 character no need to shift 
     if (str.Length < 1) throw new Exception("0 length string"); 

     if (n > str.Length) // If number is greater than the length of the string then take MOD of the number 
     { 
      n = n % str.Length; 
     } 

     StringBuilder s1=new StringBuilder(str.Substring(n,(str.Length - n))); 
     s1.Append(str.Substring(0,n)); 
     str=s1.ToString(); 


    } 

///你可以做一个使用跳跃,并采取了字符串操作的功能

public static void Rotate1(ref string str, int n) 
    { 
     if (n < 1) 
      throw new Exception("Negative number for rotation"); ; 
     if (str.Length < 1) throw new Exception("0 length string"); 

     if (n > str.Length) 
     { 
      n = n % str.Length; 
     } 

     str = String.Concat(str.Skip(n).Concat(str.Take(n))); 

    } 
0

你也可以做到这一点用一个简单的LINQ声明:

注:同可以用简单和/或同时循环来实现

string a = "ABCDEFGH"; 
a = new string(Enumerable.Range(0, a.Length).Select(i => (a[(i+1)%a.Length])).ToArray());