Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Number to an Array C#

Posted on 2006-11-30
3
238 Views
Last Modified: 2010-04-16
If I have a number, say 00153 which i have read in and it is set to a string variable 'number1'.

So number1 = 00153;

How do i convert that into an array so that each numbers is a diffrent element in the array.
0
Comment
Question by:jmkotman
3 Comments
 
LVL 1

Accepted Solution

by:
lmedwilliams earned 500 total points
ID: 18049664
I assume you are trying to break apart a single string.

Simply iterate through the string with an index.

So for example "00153" as the value for number1, written in code as number1[0] would return 0, and number1[4] would give you a 3.

Note the index starts at 0, and there is an extra LF/CR at the end (number1[5,6]), which will return a value, just not a number.

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18049916
Do you want a string array?

            string number1 = "00153";

            string[] digits = new string[number1.Length];
            for (int i = 0; i < number1.Length; i++)
            {
                digits[i] = number1.Substring(i, 1);
            }

            foreach (string digit in digits)
            {
                System.Diagnostics.Debug.WriteLine(digit);
            }

Or did you want an Array of ints?
0
 
LVL 10

Expert Comment

by:tpwells
ID: 18055730
"00153".ToCharArray() will give you a char array

if the variable is strTest then
char[] fields = strTest.ToCharArray();
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
This is about my first experience with programming Arduino.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question