views:

152

answers:

5

i have a string, something like this:

12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.

and i have to split it.

the first 8, then the next 4 and so on.

Edit: i would prefer to define the length of the pieces in a separate file. and the program to get the length form the external file.

  • Program (length1=a)
  • File (a=8)
+3  A: 

You can use String.Substring

var field1 = myString.Substring(0,8);
var field2 = myString.Substring(8,4);

and so on.

Robert Harvey
thanks for the help. can i use `var field1 =myString.Substring(a)` and `a="0, 8"`?
sebastian
@sebastian: You have to get the 0 and 8 separately from your configuration data source, and present them to the SubString methods as `int` values.
Robert Harvey
+4  A: 

To divide a string, you can use the String.SubString() method. It has an overload that accepts a starting position and a length:

string input = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";

string sub1 = input.SubString(0, 8);
string sub2 = input.SubString(8, 4);
// and so on.

To insert the data into a database you need to use ADO.NET. There are many examples online for doing this. Hopefully the link to MSDN will get you started.

LBushkin
thanks for the help.can i use `string sub1 =input.Substring(a)`?and a="0, 8"?
sebastian
That's not how method arguments work. You can, however, declare two separate variables for each parameter, and pass them in: `int a = 0; int b = 8; string sub1 = input.SubString(a,b);`. This can make it easier to implement the logic as a loop and to avoid having to hardcode the starting index for each substring operation.
LBushkin
A: 

I would first change that string into char[] and then using some extra variables print out (or whatever you need to do with it).

Vash
What does changing it into a char[] add? A string is already enumerable and indexable.
Paul Ruane
That depends of the future usage of that string. But i mouse cases rally nothing.
Vash
A: 
int index = 0;
while(index < str.Length)
{
string str1 = "";
string str2 = "";
try
{
 str1 = str.Substring(index,8);
index += 8;
str2 = str.Substring(index,4);
index+=4;
}
catch
{
// fix the end of string
break;
}
myOptionList.Add(new Option{str1, str2});
}
SaeedAlg
A: 

Here's a solution that uses LINQ to take a set of characters based on a list of specified "pages."

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//...
public static void TestStringSplit()
{
    var s = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.";
    var chars = s.ToCharArray().AsEnumerable();
    var pages = new List<int>() { 8, 4, 5, 3, 50, 50 };
    var output = new List<string>();

    for (int i = 0; i < pages.Count; i++)
    {
        var page = pages[i];

        output.Add(String.Join("",
            chars.Skip(pages.Where((p, j) => j < i).Sum(p => p)).Take(page)
            ));
    }

    output.ForEach(o => Console.WriteLine(o));
}

returns:

12345678
9012
34567
890
12345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890.

In the above example,

  • The pages variable represents the the size of each substring that you want to take from the main string.
  • The Skip LINQ expression is skipping all of the characters that have already been "paged."
  • The Take LINQ expression is returning a group of characters that match the size you are expecting.

The advantage of using an enumerable of chars is that you don't get index out of range exceptions in the Skip call like you normally would with a Substring call.

How you fill the pages variable with data and how you get the values in output into your database is dependent on your implementation, but this should give you a good start.

Ben McCormack