tags:

views:

179

answers:

4

i have a string like 123Prefix1pics.zip

i want to split it into 123 Prefix1 pics.zip and store them in different variables i m trying to do it in c#,.net jst litle bit confused on how to use split method

A: 

Look like you want to split by fixed size.

So use yourString.Substring(0, 3);

Matthieu
+6  A: 
splitArray = Regex.Split(subjectString, @"(?<=\p{N})(?=\p{L})");

will work in C# to split in positions between a number (\p{N}) and a letter (\p{L}).

If you also want to split between a letter and a number, use

splitArray = Regex.Split(subjectString, @"(?<=\p{L})(?=\p{N})|(?<=\p{N})(?=\p{L})");

however, that splits your example too much.

Tim Pietzcker
The answer is elegant but as Tim suggests, it doesn't produce the required result - yet it gets marked correct?
Kirk Broadhurst
Nothing against you Tim but I think the question is very poor, as I've said in my answer below.
Kirk Broadhurst
+1  A: 

You can start with:

string filename = "123Prefix1pics.zip"
string part1 = filename.Substring(0, 3);
string part2 = filename.Substring(3, 7);
string part3 = filename.Substring(10, 4);

You can also note String.Split() needs a separator argument, like an ; or ,. As you don't have any separator, you can try two approaches:

  • Make sure all your filenames have same format; this way, you can to use Substring() to break your strings
  • You can identify a more general pattern, as "numbers, 7 characters plus 4 more characters" and to use a regular expression. This is a more advanced solution and can lead to maintenance problems;

I recommend you to stick with first option.

Rubens Farias
+4  A: 

You only want to split that one string? Too easy!

string filename = "123Prefix1pics.zip"
string part1 = "123"
string part2 = "Prefix1"
string part3 = "pics.zip"

Ok this is a joke, but it gives the right answer. Unless you generalise your splitting rule, or provide further examples, we can only guess.

You may be asking to make a string break after a numeral, but again I'm only guessing.

Kirk Broadhurst
:) I guess we agree on this, Kirk.
Tim Pietzcker