473,378 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C# string with comma separated values into array of ints

If I start off with a string which contains comma separated ints. I
want to move this into an int array so i can do sorting.

I thought I might get this to work:

int[] test = {textBox1.Text};

What would be the best way to accomplish this?

This is what i came up with:

private void button1_Click(object sender, System.EventArgs e)
{
int [] test;

test = commaStringToArray(textBox1.Text);

// ok here i will do stuff to test[], like sorting
//

textBox2.Text = arrayToCommaString(test);
}

private int[] commaStringToArray(string strComma)
{
string [] strArray;

strArray = strComma.Split(new char[] {','});
int [] intArray = new int [strArray.Length];

for (int i = 0; i < strArray.Length; i++)
intArray[i] = int.Parse(strArray[i]);

return intArray;
}

private string arrayToCommaString(int[] intArray)
{
string strOut = "";

foreach (int i in intArray)
strOut += ", " + i.ToString();

return strOut.Remove(0,2);
}
Nov 15 '05 #1
1 33769
idog,

What you have is basically the easiest way. You can get more
performance, but the time to develop that solution could be high (you would
have to iterate through each character, parsing when you get to commas, and
then generating the integer and performing the calcs on the fly, etc, etc).

Also, unless you can guarantee that the string is in the format you want
(integer, comma, integer, etc, etc), you might want to put some error
checking code in there.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"idog" <id****@hotmail.com> wrote in message
news:c4**************************@posting.google.c om...
If I start off with a string which contains comma separated ints. I
want to move this into an int array so i can do sorting.

I thought I might get this to work:

int[] test = {textBox1.Text};

What would be the best way to accomplish this?

This is what i came up with:

private void button1_Click(object sender, System.EventArgs e)
{
int [] test;

test = commaStringToArray(textBox1.Text);

// ok here i will do stuff to test[], like sorting
//

textBox2.Text = arrayToCommaString(test);
}

private int[] commaStringToArray(string strComma)
{
string [] strArray;

strArray = strComma.Split(new char[] {','});
int [] intArray = new int [strArray.Length];

for (int i = 0; i < strArray.Length; i++)
intArray[i] = int.Parse(strArray[i]);

return intArray;
}

private string arrayToCommaString(int[] intArray)
{
string strOut = "";

foreach (int i in intArray)
strOut += ", " + i.ToString();

return strOut.Remove(0,2);
}

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RD | last post by:
S= myTxtStrean.Readline gives me a string. How do I extract the fields separated by commas for instance 1,123456,345632,7876543 1,234567890,5432,87654 (values are not each the same length) ...
3
by: mahe23 | last post by:
All, How do One convert a comma separated column from a text file into rows in oracle. I have a scenario where the list of comma separated values changes dynamically. It is like: abc, ttt,...
3
by: shark | last post by:
Hi All, i hv created a sp as Create proc P @iClientid varchar (100) as Begin select * from clients where CONVERT(VACHAR(100),iClientid) in( @iclientid)
1
by: alister joseph | last post by:
hi I have two tables, i have to combine them & a column should contain comma separated value. example 1 a 2 b 2 c 2 d 3 a
2
by: PraveenAnekalmat | last post by:
Hi everyone String totalDbString = "ABC,DEF,GHI"; String queryString = "SELECT COUNT(DISTINCT(USER_ID)) FROM EDTM_USER_DETAILS WHERE USER_ID IN ( ? )"; PreparedStatement ps =...
4
by: flavourofbru | last post by:
Hi, In the datagridview control, one of my column has multiple values separated by a comma Is there a way to get individaual values which are separated by a comma from that particular cells? ...
10
by: Tony Johansson | last post by:
Hello! What does it mean when it says that there is no information about the data types of the data extracted from a CSV file. Can somebody this. //Tony
3
by: anil2083 | last post by:
How to migrate the comma separated values from one table to another table? suppose we have table i.e XYZ and we have comma separated values in few columns i.e( column_name and values are...
5
by: amkohl | last post by:
<script type="text/javascript">dcsMultiTrack('DCS.dcsuri','/a/b/c/d/e','DCS.dcsqry','_ABC=ABC_0001','DCSext.error_code','abc500','DCSext.error_desc','FAILURE','DCSext.error_type','ABC...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.