none
Devide a string to Equal Parts RRS feed

  • Question

  • Hi

             I have a huge string which contains a XML content and i have to devide it into n parts .How can i do it in C#.
    I HAVE A requirement that after  deviding it into n-parts i want to put the values to Excel cells range from A1 to A(n) .

    How can i do it.I just want to devide it into n parts.

    Thanks
    __Das
    Dwipayan Das
    Wednesday, June 4, 2008 7:27 AM

Answers

  • Some code I just put together:

     

    1 using System;  
    2  
    3 namespace ConsoleApplication1 {  
    4     class Program {  
    5         static void Main(string[] args) {  
    6             String n = "This is my string and it is not very long but long enough";  
    7             int start = 0;  
    8             int parts = 5;  
    9             int size = n.Length / 5 + 1;  
    10  
    11             while (start < n.Length) {  
    12                 int end = start + size < n.Length ? size : n.Length - start;  
    13                 Console.WriteLine(n.Substring(start, end));  
    14                 start += size;  
    15             }  
    16  
    17             Console.ReadLine();  
    18         }  
    19     }  
    20

     


    Ron Whittle
    • Proposed as answer by Ron.Whittle Wednesday, June 4, 2008 2:35 PM
    • Marked as answer by Dwipayan Das Thursday, June 5, 2008 9:11 AM
    Wednesday, June 4, 2008 2:35 PM

All replies

  • Some code I just put together:

     

    1 using System;  
    2  
    3 namespace ConsoleApplication1 {  
    4     class Program {  
    5         static void Main(string[] args) {  
    6             String n = "This is my string and it is not very long but long enough";  
    7             int start = 0;  
    8             int parts = 5;  
    9             int size = n.Length / 5 + 1;  
    10  
    11             while (start < n.Length) {  
    12                 int end = start + size < n.Length ? size : n.Length - start;  
    13                 Console.WriteLine(n.Substring(start, end));  
    14                 start += size;  
    15             }  
    16  
    17             Console.ReadLine();  
    18         }  
    19     }  
    20

     


    Ron Whittle
    • Proposed as answer by Ron.Whittle Wednesday, June 4, 2008 2:35 PM
    • Marked as answer by Dwipayan Das Thursday, June 5, 2008 9:11 AM
    Wednesday, June 4, 2008 2:35 PM
  • Hi

          Many many thanks .Now i am able to split the string to n equal parts.

    Thanks

    __Das
    Dwipayan Das
    Thursday, June 5, 2008 9:13 AM