-2

The variable is: Hello The result I want:

split[0] = he;
split[1] = ll;
split[2] = o + ( space );

I tried this code:

string[] split = new string[text.Length / 2 + (text.Length % 2 == 0 ? 0 : 1)];
for (int i = 0; i < split.Length; i++)
{
  split[i] = text.Substring(i, i + 2 > text.Length ? 1 : 2);
}

The output is "He el lo" (it doubles the second character).

GDD Hyper
  • 7
  • 2
  • You may want to add your programming language as tag to your question. – Holger Just Aug 17 '21 at 12:00
  • 1
    Hint: run the code mentally, on a piece of paper. And note: it would help if you specified the language you are using, for example using the corresponding tag. And: you could make your life much easier if you just added a " " space in case the length of your input isnt even. – GhostCat Aug 17 '21 at 12:01
  • 1
    And hint: check out your code, and ask yourself what values `i` will go to. And what values it *should* go to. – GhostCat Aug 17 '21 at 12:02
  • I don't get why "Hello" gets split this way. Where does the extra space come from? Was it in the original string? Or is it just used as padding for when there is an odd number of characters? – Sweeper Aug 17 '21 at 12:09
  • If the variable.lenght / 2 haves decimals, then it adds space on variable – GDD Hyper Aug 17 '21 at 12:10
  • i already added this part – GDD Hyper Aug 17 '21 at 12:11
  • odd or not, it doubles the second character – GDD Hyper Aug 17 '21 at 12:11
  • Does this answer your question? [Splitting a string / number every Nth Character / Number?](https://stackoverflow.com/questions/4133377/splitting-a-string-number-every-nth-character-number) – Trevor Aug 17 '21 at 12:11
  • it became horror code for me, i cant understand it – GDD Hyper Aug 17 '21 at 12:13
  • 1
    I was [bored](https://dotnetfiddle.net/6i9rDw). – Sinatr Aug 17 '21 at 12:17

2 Answers2

0

Try this:

string input = "Hello"
string[] split = new string[input.Length / 2 + (input.Length % 2 == 0 ? 0 : 1)];
for (int i = 0; i < input.Length; i+=2)
{
    split[i/2] = input.Substring(i, i + 2 > input.Length ? 1 : 2);
}

This steps through the input string in increments of two and takes 2 characters at a time.

Slugsie
  • 851
  • 1
  • 7
  • 17
-1

I think this code will help:

 string text = "1234567";
        int loop = text.Length / 2 + (text.Length % 2 == 0 ? 0 : 1);
        List<string> split = new List<string>();
        int readTotal = 0;
        int textLen = text.Length;
        for (int i = 0; i < loop; i++)
        {
            if (textLen- readTotal >= 2)
                split.Add(text.Substring(i * 2, 2));
            else
                split.Add(text.Substring(i * 2, 1));
            readTotal += 2;

        }
Reza Akraminejad
  • 1,412
  • 3
  • 24
  • 38
  • First you have to pass parameter in Substring second method as how much text you want to read, so I pass 2 or 1 in case if I reach to end of a text with odd number of alphabets. Second I've added a readTotal variable to check how many I read and how much is to reach at the end of the string. – Reza Akraminejad Aug 17 '21 at 12:21
  • Your welcome. Please set this answer as useful – Reza Akraminejad Aug 17 '21 at 12:29