Everest
Everest

Reputation: 580

Splitting a string array

I have a string array string[] arr, which contains values like N36102W114383, N36102W114382 etc...
I want to split the each and every string such that the value comes like this N36082 and W115080.

What is the best way to do this?

Upvotes: 3

Views: 272

Answers (6)

GarethOwen
GarethOwen

Reputation: 6133

Using the 'Split' and 'IsLetter' string functions, this is relatively easy in c#.

Don't forget to write unit tests - the following may have some corner case errors!

    // input has form "N36102W114383, N36102W114382"
    // output: "N36102", "W114383", "N36102", "W114382", ...
    string[] ParseSequenceString(string input)
    {
        string[] inputStrings = string.Split(',');

        List<string> outputStrings = new List<string>();

        foreach (string value in inputstrings) {
            List<string> valuesInString = ParseValuesInString(value);
            outputStrings.Add(valuesInString);
        }

        return outputStrings.ToArray();
    }

    // input has form "N36102W114383"
    // output: "N36102", "W114383"
    List<string> ParseValuesInString(string inputString)
    {
        List<string> outputValues = new List<string>(); 
        string currentValue = string.Empty;
        foreach (char c in inputString)
        {
            if (char.IsLetter(c))
            {
                if (currentValue .Length == 0)
                {
                    currentValue += c;
                } else
                {
                    outputValues.Add(currentValue);
                    currentValue = string.Empty;
                }
            }
            currentValue += c;
        }
        outputValues.Add(currentValue);
        return outputValues;
    }

Upvotes: 0

Tom
Tom

Reputation: 568

in case you're only looking for the format NxxxxxWxxxxx, this will do just fine :

Regex r = new Regex(@"(N[0-9]+)(W[0-9]+)");

Match mc = r.Match(arr[i]);
string N = mc.Groups[1];
string W = mc.Groups[2];

Upvotes: 0

ControlPower
ControlPower

Reputation: 610

If you use C#, please try:

String[] code = new Regex("(?:([A-Z][0-9]+))").Split(text).Where(e => e.Length > 0 && e != ",").ToArray();

Upvotes: 0

Kevin Hsu
Kevin Hsu

Reputation: 1756

Forgive me if this doesn't quite compile, but I'd just break down and write the string processing function by hand:

public static IEnumerable<string> Split(string str)
{
    char [] chars = str.ToCharArray();
    int last = 0;
    for(int i = 1; i < chars.Length; i++) {
        if(char.IsLetter(chars[i])) {
            yield return new string(chars, last, i - last);
            last = i;
        }
    }

    yield return new string(chars, last, chars.Length - last);
}

Upvotes: 0

Narendra Yadala
Narendra Yadala

Reputation: 9664

This should work for you.

Regex regexObj = new Regex(@"\w\d+"); # matches a character followed by a sequence of digits
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
    matchResults = matchResults.NextMatch(); #two mathches N36102 and W114383
}

Upvotes: 1

Andrey Atapin
Andrey Atapin

Reputation: 7955

If you have the fixed format every time you can just do this:

string[] split_data = data_string.Insert(data_string.IndexOf("W"), ",")
    .Split(",", StringSplitOptions.None); 

Here you insert a recognizable delimiter into your string and then split it by this delimiter.

Upvotes: 0

Related Questions