Hey all, I'm trying to split a string of characters and after a day of trying every possible way of splitting a string into groups of 7, I'm still hopelessly stuck!

Bascially, the user enters/pastes a string into "txtArea" (the first text box), then click an encrypt button... the program then encrypts the string from txtArea into a string in txtArea2... Without giving away the heart of my program, I'll give you an idea of what's found in txtArea2:

This is exactly how the string is presented in txtArea2:
>><>><<<>>><><<>>>><<<<<>><<

This is how the string looks per original character:
>><>><< <>>><>< <>>>><< <<<>><<

Now, my problem comes with not being able to split the entire original txtArea2 string (up to and over 50,000 characters) into groups of strings consisting of 7 characters ie:
>><>><<

The only way to decrypt the encrypted string is to have it in a block of 7 characters (still string data type, I'm not using char or char[] data types)

Another possible solution would be to pad each block of 7 with spaces either side:
... >><>><< ....

This would be an easier way to decrypt using my exe...

Sorry to ask such a standard question! I'm just really stuck! I've tried quadrillions of different ways of doing this but still haven't come up with a solution! I'm trying to stay away from implementing embedded classes so a few lines of code would really sort this out!

int x = 0;
            while (x != txtArea2.Text.Length)
            {
                int i;
                int j = i + 7;
                for (i = 0; i < txtArea2.Text.Length; i++)
                {
                    string splitter = txtArea2.Text.Substring(i, j);
                    txtArea2.Text = txtArea2.Text + splitter.ToString();
                }                
            }

I've also tried:

.Substring(0, 7)
.Substring(i, j)

(Where i and j represent the start and finishing index of each 7 group)

NOTHING though, SO stuck!

I'd really appreciate a little bit of help with this little problem!

Thanks everyone who reads!! Take care

Rob

Dani AI

Generated

A few likely causes of the hang and wrong output in the original post: the C# Substring overload expects (startIndex, length) (not an end index), loop variables were being used before initialization or not updated, and the code was repeatedly mutating txtArea2.Text while iterating (which changes Length mid-loop). 's two ideas (chunking by index, and inserting every N chars) are valid approaches; for large inputs and predictable output it's safer to build a new string and assign it once.

A compact, readable alternative is to treat each 1–7 character match and replace it with a space, the 7-char block (right-padded if needed), and another space. This avoids shifting indexes and keeps the final block properly padded:

using System.Text.RegularExpressions;

string input = txtArea2.Text;
string output = Regex.Replace(input, ".{1,7}", new MatchEvaluator(m => " " + m.Value.PadRight(7) + " "));
txtArea2.Text = output;

Notes and cautions:

  • Do not modify the source textbox inside the loop; build the result in a separate variable and assign it once.
  • If the payload can contain spaces, using a plain space as a separator may confuse decryption; consider a unique delimiter or fixed-width padding (PadRight) as shown.
  • For very large strings and maximum throughput, a StringBuilder loop that appends blocks is slightly faster and uses less intermediate memory than regex; the regex version is concise and clear for most sizes (50k is fine).
  • Verify the decryption expects the exact padding format (leading/trailing space per 7-char block) before committing to the scheme.

Recommended Answers

All 4 Replies

Member Avatar for Member #46692

Another possible solution would be to pad each block of 7 with spaces either side:

I'd go for that and then use string.split() using the space as a delimiter.

I dont know what you are trying to do but i made this for you.
There might be a better way but this adds chars in groups of 7 to a string.

string src = textBox1.Text;//source 
            string dest = "";//hold output
            int len = 7;//the length of sections

            //loop in increments of our section lengths
            for (int i = 0; i <= src.Length - 1; i += len)
            {
                //prevents out of range exception, 
                if (src.Length - i < len)
                    { len = src.Length - i; }
                
                //this substring is the sections
                //so for this it adds a substring of 7 length to our output.
                dest += src.Substring(i, len);

            }
            textBox2.Text = dest;

Thanks for your help! It's still not working but I really appreciate your efforts!

The only way I can make my problem easier to understand is this:

I need to get from this:
"abcdefghijklmnopqrstuvwxyz012345678"

To this:
" abcdefg hijklmn opqrstu vwxyz01 2345678 "
(A space padding each group of 7 characters)
(This won't be the string obviously, the string still comes from txtArea2.Text)

Thanks again!

static void Main(string[] args)
        {
            string s = "123456789012345678901234567890";//source string
            int i=7;//index
            do
            {
                s = s.Insert(i, " "); //insert the space
                i += 8; //update index up 7 plus 1 for new space
            }
            while (i < s.Length);//while the index is less then the length
            Console.Write(s);//finished
            Console.Read();
        }

So you only want to insert a space every 7 spaces? this will do that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.