3

I have the string(10325710) and I want to split the string into an array. After I split the string into an array, the array will be {1,0,3,2,5,7,1,0}. Note there are two 1s and two 0s in the string. I don't want to split the '1' and the '0'. Therefore, the array I wish to get is {10,3,2,5,7,10}.

Any advice?

my C# code:

string myNumber = "10325710";
string[] myArray = myNumber.Select(p => p.ToString()).ToArray();
4
  • 1
    How do you know there's not a 32 in the array? Or is it just that there are no zeroes? Commented Jan 8, 2013 at 10:52
  • 1
    I guess, that only values 1..10 are in the string. That should be stated explicitly, though. Commented Jan 8, 2013 at 10:55
  • What's the range of valid integers allowed in the output array? Commented Jan 8, 2013 at 10:56
  • ya...there is only 1-10 in myNumber. Commented Jan 9, 2013 at 1:16

4 Answers 4

16
int[] nums = myNumber.Replace("10", "A")
                     .Select(c => Convert.ToInt32(c.ToString(), 16))
                     .ToArray();

Basically replacing "10"s with "A"s, and then treating each character as a base-16 number.

Sign up to request clarification or add additional context in comments.

5 Comments

@Spook not sure what you mean by expandable. The question sounds like they just need to parse numbers 1-10. Also, you'd usually downvote an answer because it is wrong, not because you don't think it's perfect;)
@ErenErsönmez Maintainable may be a better word here. Usually, you can solve the problem in thousands of ways, so its quality cannot be measured by if it solves the problem or not, but how maintainable (easy to fix or modify) it is. Suppose, that OP wants to interpret 20s and 30s as well. All other solutions can be modified (most of them quite easily) to suit new needs. This one has to be rewritten from scratch (even though it's a short one). Thinking out of box is very good, and the trick is indeed cool. But I think, that it is not the way the problem should be solved.
well then what if OP wants to interpret 100s, 1000s, telephone numbers...? so you downvote a perfectly valid answer because it doesn't satisfy some requirements that don't even exist? :)
This is answer giving valid results, but IMO is not valid itself. By the way, out of curiosity, I profiled the solutions for 500k-length string. Yours is fastest (400ms) and consumes 137M of RAM. Dominic's takes 4.3s and 222M, Ken's 5.1s and 712M and mine (modified) - 1.2s and 61M. ideone.com/GC1lTk
@Spook of the top of my head, you might get some improvement by adding .AsParallel().AsOrdered() right before the .Select(). I haven't thought about parallelizing String.Replace though, that could be an interesting exercise.
4

Well, just to put some regex on the table:

Regex.Split(myNumber, @"(?!^)(?=[1-9])");

Comments

1

statement:

var myArray=Regex.Matches(myNumber, @"1?\d").Cast<Match>().Select(p => p.ToString()).ToArray();

code:

var myNumber="98765432101032571001210345610789";
var myMatches=Regex.Matches(myNumber, @"1?\d").Cast<Match>();
var myArray=myMatches.Select(p => p.ToString()).ToArray();

var whats_in_myArray="{ "+String.Join(", ", myArray)+" }";
Debug.Print("{0}", whats_in_myArray);

output:

{ 9, 8, 7, 6, 5, 4, 3, 2, 10, 10, 3, 2, 5, 7, 10, 0, 12, 10, 3, 4, 5, 6, 10, 7, 8, 9 }

Comments

1

You can do it manually quite easily.

int i = 0;

int len = 0;
for (int j = 0; j < s.Length; j++)
    if (s[j] != '0')
        len++;

int[] result = new int[len];
int index = 0;
while (i < s.Length)
{
    if (i < s.Length - 1 && int.Parse(s[i + 1].ToString()) == 0)
    {
        result[index++] = int.Parse(s[i].ToString()) * 10;
        i += 2;
    }
    else
    {
        result[index++] = int.Parse(s[i].ToString());
        i++;
    }
}

return result;

7 Comments

i like this approach, much easier to fiddle with in the future if the requirements change +1
Are you sure this code works for this case? Because if (i < myNumber.Length && myNumber[i + 1] == 0) line should thrown OutOfRangeException.
It doesn't even compile since myNumber seems to be the string and int.Parse(myNumber[i]) don't work with chars. It also doesn't yield the correct result.
Seem to work now. Undone my downvote, however, i would use int.Parse(myNumber[i].ToString()) instead of (int)Char.GetNumericValue(myNumber[i]).
I wonder, which one would use less memory. That's a matter of code optimization, but I guess, that .ToString() may instantiate a string instance, while GetNumericValue shouldn't use any additional memory and that's why I chose the latter.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.