Solved

Number to an Array C#

Posted on 2006-11-30
3
215 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
Comment Utility
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
Comment Utility
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
Comment Utility
"00153".ToCharArray() will give you a char array

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 84
pairs challenge 5 44
drawing animated level bar based on numbers 3 70
recursion example 16 65
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now