Sybren
Sybren

Reputation: 1079

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. The result needs to be shown in a textfield called 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 n m d text || text is padded with spaces until its length is a multiple of n || the characters in text are circulary shifted in the alphabet by the displacement d || example: if d = 1 then 'a' -> 'b' , 'b' -> 'c' .... etc... 'z' -> 'a' || text is divided in blocks of length n characters || inside every block of n the characters are circulary shifted m times to the left || the shifted groups are concatenated

I already solved the m and d only have to solve the n.


The solutions given above aren't completely doing the job for me, maybe it helps when I post the exercise:

|| crypt n m d text || text is padded with spaces until its length is a multiple of n || the characters in text are circulary shifted in the alphabet by the displacement d || example: if d = 1 then 'a' -> 'b' , 'b' -> 'c' .... etc... 'z' -> 'a' || text is divided in blocks of length n characters || inside every block of n the characters are circulary shifted m times to the left || the shifted groups are concatenated

I already solved the m and d only have to solve the n.

Upvotes: 2

Views: 670

Answers (3)

acermate433s
acermate433s

Reputation: 2544

Here is a LinqPad script:

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();

}

Upvotes: 0

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391286

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.

Upvotes: 4

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

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. 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.

If you then wanted that string in an array, because I see you have one at the end, you could do this:

invoerstring.ToArray();

Upvotes: 1

Related Questions