【问题标题】:How to reverse a string 2character and 2character from the end c#如何从结尾 c# 反转字符串 2 个字符和 2 个字符
【发布时间】:2020-07-26 15:18:06
【问题描述】:

示例string:

A3148579

预期结果:

798514A3

我试过这段代码:

 public static string Reverse(string s)
 {
     char[] charArray = s.ToCharArray();

     Array.Reverse(charArray);

     return new string(charArray);
 }

实际结果是9758413A

但我想要 798514A3

谢谢大家

【问题讨论】:

标签: c# .net algorithm


【解决方案1】:
You can try below code. This is just to give you a idea and you can update based on the test cases and requirement. Below code works fine for your input which you have mentioned. I have not considered if the length is odd. You can do your research and update logic which will help you to learn and know more.

string input = "A3148579";
            Stack stack = new Stack();
            int count = 0;
            string output = "";

            for (int i = 0; i < input.Length/2; i++)
            {
                stack.Push(input.Substring(count, 2));
                count = count + 2;
            }

            while (stack.Count > 0)
            {
                output += stack.Pop().ToString();
            }

【讨论】:

    【解决方案2】:

    您可以将初始字符串拆分为size = 2 的块(例如,在Substring 的帮助下);请注意 ToCharArray() 返回 chars 即 size = 1 的块。

    让我们概括一下解决方案:现在我们有 size 个块

    代码:

    using System.Linq;
    
    ...
    
    public static string Reverse(string s, int size = 2) {
      if (size < 1)
        throw new ArgumentOutOfRangeException(nameof(size));
    
      if (string.IsNullOrEmpty(s))
        return s;
      
      int n = s.Length / size + (s.Length % size == 0 ? 0 : 1);
    
      return string.Concat(Enumerable
        .Range(0, n)
        .Select(i => i < n - 1 ? s.Substring(i * size, size) : s.Substring(i * size))
        .Reverse());
    }
    

    演示:

    Console.Write(Reverse("A3148579"));
    

    结果:

    798514A3
    

    【讨论】:

      【解决方案3】:

      C# Example: Split An Array into Multiple Smaller Arrays

      使用 Lambda 表达式扩展 Array 类以包含一个名为 Split 的新方法:

      public static class MyArrayExtensions
      {
          /// <summary>
          /// Splits an array into several smaller arrays.
          /// </summary>
          /// <typeparam name="T">The type of the array.</typeparam>
          /// <param name="array">The array to split.</param>
          /// <param name="size">The size of the smaller arrays.</param>
          /// <returns>An array containing smaller arrays.</returns>
          public static IEnumerable<IEnumerable<T>> Split<T>(this T[] array, int size)
          {
              for (var i = 0; i < (float)array.Length / size; i++)
              {
                  yield return array.Skip(i * size).Take(size);
              }
          }
      }
      

      测试新的数组类方法:

      [TestMethod]
      public void TestSplit2()
      {
         string str = "A3148579";
      
          char[] array1 = new char[str.Length];
      
          for (int i = 0; i < array1.Length; i++)
          {
              array1[i] = i;
          }
      
      
          // Split into smaller arrays of maximal 2 elements
          IEnumerable<IEnumerable<int>> splited = array1.Split<int>(2);
          int j = 0;
          foreach (IEnumerable<int> s in splited){
              j++;
          }
          log.InfoFormat("Splitted in to {0} smaller arrays.", j);
      }
      

      最后你只需要反转结果数组(更小)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-02
        • 2014-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多