-3

If a message is longer than a 160 characters I will need to display the message in groups of a 153 characters.

So the 161 char string should display in two groups(one group with a 153 characters and the other with 8 characters). How do I do this?

string message = "0123456789";//10char string
//string message = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";//161char string
int parts = (message.Length <= 160) ? 1 : ((message.Length + 152) / 153);
for (int x = 1; x <= parts; x++)
{                                            
    Console.WriteLine("this Msg Part: " + x.ToString());
    Console.WriteLine("this Msg Part Text:" + "//messgae part text goes here...");
    Console.WriteLine("Total Msg Parts: " + parts.ToString());
}
BossRoss
  • 869
  • 2
  • 11
  • 31
  • Haven't you got the answer in your previous post? – Microsoft DN Nov 19 '13 at 11:42
  • What error are you getting? – rhughes Nov 19 '13 at 11:44
  • [Split String into smaller Strings by length variable](http://stackoverflow.com/questions/3008718/split-string-into-smaller-strings-by-length-variable)?? – huMpty duMpty Nov 19 '13 at 11:44
  • 1
    You asked this question today and it was closed, why are you posting same answer 2h later?! – Kamil Budziewski Nov 19 '13 at 11:45
  • @Wudzik because I did not get an answer apparently my question was not suitable for most readers.... Hence the question again worded slightly differently... – BossRoss Nov 19 '13 at 11:47
  • 1
    @BossRoss You did not get an answer, because your question was (and still is) inclear and does not provide any effort. Try to improve your question. Posting it until you get answers is not a good idea – Kamil Budziewski Nov 19 '13 at 11:48
  • 1
    @BossRoss: Posting the same question is not what you should do!!! [How do I get attention for old, unanswered questions?](http://meta.stackexchange.com/questions/7046/how-do-i-get-attention-for-old-unanswered-questions) – huMpty duMpty Nov 19 '13 at 11:48
  • huMpty duMpty: Noted-thank you for the helpful post. @wudzik: I believe the word you looking for is unclear :-) and is clarity not in the eye of the beholder? Amazing other people understood my question-people with less reputation than you! Imagine putting your negative energy/comments into helping others-you could change the world. – BossRoss Nov 19 '13 at 12:09
  • @BossRoss "inclear" was a typo (u is just next to i). http://stackoverflow.com/help/how-to-ask \\ BTW Having high reputation does not mean that you have to understand every unclear question. Pointing that question can be unclear is also helping ... There is a reason this question has -4 (so far) score – Kamil Budziewski Nov 19 '13 at 12:14

3 Answers3

2
string message = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";//161char string
int groupCount = 153;

IEnumerable<string> groups = message.Select((c, index) => new{ c, index })
            .GroupBy(x => x.index / groupCount)
            .Select(xg => string.Join("", xg.Select(x => x.c)));

foreach(string str in groups)
    Console.WriteLine(str);

prints:

"012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012" 
"34567890"
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Consider the following code...

        string message = "0123456789";//10char string
        //string message = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";//161char string
        int parts = (message.Length/ 153) + (message.Length % 153 > 0 ? 1 : 0);
        for (int x = 1; x <= parts; x++)
        {
            Console.WriteLine("this Msg Part: " + x.ToString());
            Console.WriteLine("this Msg Part Text:" + message.Substring( (x-1)* 153, message.Length < ((x-1)* 153 + 153) ? message.Length - (x-1)* 153 : 153));
            Console.WriteLine("Total Msg Parts: " + parts.ToString());
        }

Good Luck!

gpmurthy
  • 2,397
  • 19
  • 21
0

Change your main line something like

Console.WriteLine("this Msg Part Text:" + message.Substring(((x-1)*153),(parts == x )? message.Length-(153*(x-1)) : 153*x));

Hope this will help...

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Rohit
  • 69
  • 1
  • 7