-3

我有一种情况,我的字符串不能超过某个点,所以我想要做的就是把它切成更小的“x”字符字符串,然后一个一个地打印出来。它们并不都需要相等,如果 x 为 5,并且我有一个 11 个字符的字符串,打印 3 行 5、5 和 1 个字符就可以了。有没有一种简单的方法可以在 C# 中做到这一点?

例子:

string Test = "This is a test string";
stringarray = Cutup(Test, 5); 
//Result:
//"This "
//"is a "
//"test "
//"strin"
//"g"
4

10 回答 10

2

尝试这样的事情:

    public string[] Cutcup(string s, int l)
    {
        List<string> result = new List<string>();

        for (int i = 0; i < s.Length; i += l)
        {
            result.Add(s.Substring(i, Math.Min(5, s.Substring(i).Length)));
        }

        return result.ToArray();
    }
于 2013-10-24T18:51:54.007 回答
0

你可以剪断字符串然后做一个 test.lastIndexOf(' '); 如果这有帮助

于 2013-10-24T18:53:11.407 回答
0

您可以使用字符串操作函数Substring()和 for 循环来完成此操作。

于 2013-10-24T18:54:54.753 回答
0

这是一个相当 LINQy 的单线:

static IEnumerable<string> SliceAndDice1( string s , int n )
{
  if ( s == null ) throw new ArgumentNullException("s");
  if ( n < 1 ) throw new ArgumentOutOfRangeException("n");

  int i = 0 ;
  return s.GroupBy( c => i++ / n ).Select( g => g.Aggregate(new StringBuilder() , (sb,c)=>sb.Append(c)).ToString() ) ;
}

如果这让您头疼,请尝试更简单的

static IEnumerable<string> SliceAndDice2( string s , int n )
{
  if ( s == null ) throw new ArgumentNullException("s") ;
  if ( n < 1 ) throw new ArgumentOutOfRangeException("n") ;

  int i = 0 ;
  for ( i = 0 ; i < s.Length-n ; i+=n )
  {
    yield return s.Substring(i,n) ;
  }
  yield return s.Substring(i) ;

}
于 2013-10-24T19:57:43.247 回答
0

这是一个例子

int maxChars = 5;
            String myStr = "This is some text used in testing this method of splitting a string and just a few more chars and the string is complete";
            List<String> mySubStrings = new List<String>();
            while (myStr.Length > maxChars)
            {
                mySubStrings.Add(myStr.Substring(0,maxChars));
                myStr = myStr.Substring(maxChars);
            }
            mySubStrings.ToArray();
于 2013-10-24T19:01:02.867 回答
0
        List<string> result = new List<string>();
        string testString = "This is a test string";
        string chunkBuilder = "";
        int chunkSize = 5;
        for (int i = 0; i <= testString.Length-1; i++)
        {
            chunkBuilder += testString[i];

            if (chunkBuilder.Length == chunkSize || i == testString.Length - 1)
            {
                result.Add(chunkBuilder);
                chunkBuilder = "";
            }
        }
于 2013-10-24T19:01:50.900 回答
0

另一个尝试,使用更少的字符串连接

string Test = "This is a test string";
List<string> parts = new List<string>();
int i = 0; 
do
{
   parts.Add(Test.Substring(i,System.Math.Min(5, Test.Substring(i).Length))); 
   i+= 5;
} while (i < Test.Length);
于 2013-10-24T19:05:15.733 回答
0

这里有更多的方法。 Cutup2下面更有效但不太漂亮。两者都通过了给定的测试用例。

private static IEnumerable<string> Cutup(string given, int chunkSize)
{
    var skip = 0;
    var iterations = 0;
    while (iterations * chunkSize < given.Length)
    {
        iterations++;                
        yield return new string(given.Skip(skip).Take(chunkSize).ToArray());
        skip += chunkSize;
    }
}

private static unsafe IEnumerable<string> Cutup2(string given, int chunkSize)
{
    var pieces = new List<string>();
    var consumed = 0;

    while (consumed < given.Length)
    {
        fixed (char* g = given)
        {
            var toTake = consumed + chunkSize > given.Length 
                         ? given.Length - consumed 
                         : chunkSize;

            pieces.Add(new string(g, consumed, toTake));
        }

        consumed += chunkSize;
    }

    return pieces;
}
于 2013-10-24T19:18:36.623 回答
0

我曾经做过一个可用于此的扩展方法:

    public static IEnumerable<IEnumerable<T>> Subsequencise<T>(this IEnumerable<T> input, int subsequenceLength)
    {
        var enumerator = input.GetEnumerator();
        SubsequenciseParameter parameter = new SubsequenciseParameter { Next = enumerator.MoveNext() };
        while (parameter.Next)
            yield return getSubSequence(enumerator, subsequenceLength, parameter);

    }


    private static IEnumerable<T> getSubSequence<T>(IEnumerator<T> enumerator, int subsequenceLength, SubsequenciseParameter parameter)
    {
        do
        {
            yield return enumerator.Current;
        } while ((parameter.Next = enumerator.MoveNext()) && --subsequenceLength > 0);
    } 

    // Needed to let the Subsequencisemethod know when to stop, since you cant use out or ref parameters in an yield-return method.
    class SubsequenciseParameter
    {
        public bool Next { get; set; }
    }

那么你可以这样做:

string Test = "This is a test string";
stringarray = Test.Subsequencise(5).Select(subsequence => new String(subsequence.Toarray())).Toarray();
于 2013-10-24T19:20:44.353 回答
0

一条龙

        var size = 5;

        var results = Enumerable.Range(0, (int)Math.Ceiling(test.Length / (double)size))
                                .Select(i => test.Substring(i * size, Math.Min(size, test.Length - i * size)));
于 2013-10-24T19:22:02.343 回答