简体   繁体   English

在可变数量的字符后分割字符串

[英]Split string after variable amount of characters

I want to split my string called: invoerstring after a variable amount of characters (n is the number of characters when the string needs to be split).. If the string length is shorter then the variable n, spaces need to be added until the string length = n. 我想在可变数量的字符后分割名为invoerstring的字符串(n是需要分割字符串时的字符数)。如果字符串长度短于变量n,则需要添加空格,直到字符串长度= n。 The result needs to be shown in a textfield called uitvoer. 结果需要显示在名为uitvoer的文本字段中。

This is what so far: 到目前为止,这是什么:

string invoerstring = invoer.Text;

if (invoerstring.Length < n)
{
    invoerstring += "";
    char[] myStrChars = invoerstring.ToCharArray();
}

if (invoerstring.Length == n)
{
    string [] blok = invoerstring.Split();
    foreach (string word in blok)
    {
        uitvoer.Text = word;
    }
}

EDIT: The solutions given above aren't completely doing the job for me, maybe it helps when I post the exercise: 编辑:上面给出的解决方案并不能完全为我完成工作,也许在我发布练习时会有所帮助:

|| || crypt nmd text || 加密nmd文字|| text is padded with spaces until its length is a multiple of n || 文本用空格填充,直到其长度为n ||的倍数。 the characters in text are circulary shifted in the alphabet by the displacement d || 文本中的字符在字母表中循环位移d || example: if d = 1 then 'a' -> 'b' , 'b' -> 'c' .... etc... 'z' -> 'a' || 例如:如果d = 1,则'a'->'b','b'->'c'....等等...'z'->'a'|| text is divided in blocks of length n characters || 文本分为长度为n个字符的块|| inside every block of n the characters are circulary shifted m times to the left || 在n个块中的每个字符向左||循环m次循环移动 the shifted groups are concatenated 转移的组是串联的

I already solved the m and d only have to solve the n. 我已经解决了m和d,只需要解决n。


The solutions given above aren't completely doing the job for me, maybe it helps when I post the exercise: 上面给出的解决方案并不能完全为我完成工作,也许对我发布练习有帮助:

|| || crypt nmd text || 加密nmd文字|| text is padded with spaces until its length is a multiple of n || 文本用空格填充,直到其长度为n ||的倍数。 the characters in text are circulary shifted in the alphabet by the displacement d || 文本中的字符在字母表中循环位移d || example: if d = 1 then 'a' -> 'b' , 'b' -> 'c' .... etc... 'z' -> 'a' || 例如:如果d = 1,则'a'->'b','b'->'c'....等等...'z'->'a'|| text is divided in blocks of length n characters || 文本分为长度为n个字符的块|| inside every block of n the characters are circulary shifted m times to the left || 在n个块中的每个字符向左||循环m次循环移动 the shifted groups are concatenated 转移的组是串联的

I already solved the m and d only have to solve the n. 我已经解决了m和d,只需要解决n。

Here's the code you want, no need to go through a character array: 这是您想要的代码,无需遍历字符数组:

public static string EnsureExactLength(this string s, int length)
{
    if (s == null)
        throw new ArgumentNullException("null");

    return s.PadRight(length).Substring(0, length);
}

You call it like this: 您这样称呼它:

string s = "Test string".EnsureExactLength(4); // will return "Test"
string s = "Te".EnsureExactLength(4);          // will return "Te  "

You can find an example LINQPad program here . 您可以在此处找到示例LINQPad程序。

Okay, I'm honestly not sure what the code you have above is doing because I see calls like Split() without any parameters, and stuff. 好的,老实说,我不确定您上面的代码在做什么,因为我看到像Split()这样的调用没有任何参数和其他东西。 But to meet the requirements, this one line should do: 但是为了满足要求,这一行应该做到:

string invoerstring = invoer.Text.PadRight(n, ' ').Substring(0, n);

the PadRight will make sure it's as long as n and the Substring will then return the portion of the string up to n . PadRight将确保它n一样长,然后Substring将返回string直到n

If you then wanted that string in an array, because I see you have one at the end, you could do this: 如果您随后想要将该字符串放入数组中,因为我看到结尾处有一个,则可以执行以下操作:

invoerstring.ToArray();

Here is a LinqPad script: 这是一个LinqPad脚本:

void Main()
{
    const string TEXT = "12345ABCDE12345ABCDE1234";
    const int LENGTH = 5;
    const char PAD = '#';

    Enumerable.Range(0, TEXT.Length / LENGTH)
        .Union(TEXT.Length < LENGTH ? new int[] { 1 } : new int[] {})
        .Select((index, item) => TEXT.Length < LENGTH 
                                    ? TEXT.PadRight(LENGTH, PAD)
                                    : TEXT.Substring(index * LENGTH, LENGTH))
        .Concat(TEXT.Length % LENGTH != 0 
                    ? new string[] { TEXT.Substring(TEXT.Length - (TEXT.Length % LENGTH)).PadRight(LENGTH, PAD) }
                    : new string[] { })                                 
        .Dump();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM