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

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

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.