Solved

Number to an Array C#

Posted on 2006-11-30
3
250 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
[X]
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
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 86

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
Using VBscript & RegEx, how can I match lines starting with Oracle word 17 104
Ruby or Python 7 132
Data Analysis 7 59
AD Computer Objects in VB .Net 2 19
A short article about problems I had with the new location API and permissions in Marshmallow
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

751 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