将字符串拆分为两个字符对时出错(Codewars c#)

问题描述 投票:0回答:3

这是我正在研究的问题!

分割字符串

完成解决方案,以便将字符串拆分为两个字符对。如果字符串包含奇数个字符,则应使用下划线 ('_') 替换最后对中缺少的第二个字符。

示例:

SplitString.Solution("abc"); // should return ["ab", "c_"]
SplitString.Solution("abcdef"); // should return ["ab", "cd", "ef"]

我的代码:

namespace Solution 
{
  using NUnit.Framework;
  using System;  
  
  [TestFixture]
  public class SplitStringTests
  {
    [Test]
    public void BasicTests()
    {
      Assert.AreEqual(new string[] { "ab", "c_" }, SplitString.Solution("abc"));
      Assert.AreEqual(new string[] { "ab", "cd", "ef" }, SplitString.Solution("abcdef"));
    }
  }
}
public class SplitString
{
  public static string[] Solution(string str)
  {
    string[] split = new string[str.Length / 2 + (str.Length % 2 == 0 ? 0 : 1)];
         
            for (int i = 0; i < split.Length; i++)
            {

                split[i] = str.Substring(i * 2, i * 2 + 2 > str.Length ? 1 : 2);
                if (split[i].Length % 2 != 0)
                {
                    split[i] += "_";
                }

            }
            string result = string.Join(",", split);
            
            return result;
  }
}

错误

Cannot implicitly convert type 'string' to 'string[]'
c#
3个回答
0
投票

我试过了,也许对你有帮助! 代码可以进一步重构

   using System;
using System.Collections.Generic;
using System.Linq;

namespace CodeWars
{
    class Program
    {
        static void Main(string[] args)
        {
            var getList = getPairedList();
            foreach (var item in getList)
            {
                Console.WriteLine(item);
            }
        }

        public static List<string> getPairedList()
        {
            string[] cars = { "Volvo", "BMW", "Ford", "Mazda" };
            List<string> returningList = new List<string>();
            List<string> returningListNew = new List<string>();
            foreach (var item in cars)
            {
                var stringArray = item.ToList();
                int count = 1;
                string map = "[";
                if ((item.Length % 2) == 1)
                {
                    stringArray.Add(("_").ToCharArray().First());
                }
                int checkLength = 1;
                foreach (var arrItem in stringArray)
                {
                    if (count <= 2)
                    {
                        map += "" + arrItem + "";
                        if (count == 2)
                        {
                            count = 0;
                            if (checkLength == stringArray.Count)
                            {
                                map += "]";
                            }
                            else
                            {
                                map += ",";
                            }
                            returningList.Add(map);
                            map = string.Empty;
                        }
                    }
                    checkLength++;
                    count++;
                }
                string output = string.Join("", returningList.ToArray());
                returningList = new List<string>();
                returningListNew.Add(output);
            }
            return returningListNew;
        }
    }
}


0
投票

字符串 cadena = "nuevacadena"; 列表 lstCadena = new List();

            int x1 = cadena.Length / 2;
            cadena = cadena.Length % 2 == 0 ?  cadena : cadena + "_";
            
            string[] newArray = new string[x1];

            for (int i = 0; i < x1; i++)
            {
                lstCadena.Add(cadena.Substring(0, 2));
                cadena = cadena.Remove(0, 2);
            }

            newArray = lstCadena.ToArray();

-1
投票

只是为了好玩:

        var s = "abcdef";
        var res = s.SelectMany((x, i) => i % 2 == 0 ? new string[] { $"{x}{(i < s.Length-1 ? s[i + 1] : '_')}" }  : new string[0]).ToArray();
© www.soinside.com 2019 - 2024. All rights reserved.