Wrox Programmer Forums
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2005, 08:05 PM
Registered User
 
Join Date: Nov 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Char array to String

Hello all,

I get passed an array of strings (20 characters long each) to my function, I need to divide each string it in two exact halves, trim them and convert them to integers. Add them and return the result as an array of integers.

This is thus far what I've done.

//Declaring variables
char [] charTemp = new char[20];
char [] charOne = new char[10] ;
char [] charTwo = new char[10];
string strOne;
string strTwo;
char [] trimChar = {' '};
int intTmp = 0;

//strayFile is the array of strings I get.
string strTemp;
strTemp = strayFile[0];
charTemp = strTemp.ToCharArray();

//Operating first part of the string.
for (int j=0; j < 10; j++)
        charOne[j] = charTemp[j];
AND IF I DO
    strOne = charOne.ToString();
        strOne = strOne.Trim(trimChar);
        intTmp = Convert.ToInt32(strOne);
the value for strOne shows in red as "System.Char[]"
and when it assigns the value in strOne to intTmp it bombs with an unhandled exception ofo type 'System.FormatException'.

IF I DO
    strOne = Convert.ToString(charOne);
    strOne = strOne.Trim(trimChar);
    intTmp = Convert.ToInt32(strOne);
It eats it again... same way... same exception...

And that is as far as I get.

Is there a smart way to do this?

Cheers!

SDAuggie...
 
Old March 19th, 2005, 12:03 PM
Registered User
 
Join Date: Nov 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well,
Thanks for your massive amount of support ideas and encouragement.

This is what I did to solve my problem, which BTW figured out an hour after posting my original message...


int [,] intayToInt = new int[100000, 2];
char [] charTemp = new char[20];
string strTemp;
string strOne;
string strTwo;
char [] trimChar = {' '};

//Loading the array of integers from the array of strings
strTemp = strayFile[0];

for(int i = 0; strTemp != null; i++)
  {
  strTemp = strayFile[i];

    if (strTemp != null)
      {
    //Operating first part of the string.
    strOne = strTemp.Substring(0,10);
    strOne = strOne.Trim(trimChar);
    intayToInt[i,0] = Convert.ToInt32(strOne);

    //Operating second part of the string.
    strTwo = strTemp.Substring(10,10);
    strTwo = strTwo.Trim(trimChar);
    intayToInt[i,1] = Convert.ToInt32(strTwo);
      }//end if
  }//end of for

return intayToInt;

sdauggie...





Similar Threads
Thread Thread Starter Forum Replies Last Post
char[...] or string.charAt(...) Ibn_Aziz Java Basics 1 June 14th, 2006 07:08 PM
getting the last CHAR of a string kondapally Crystal Reports 0 December 13th, 2004 03:39 PM
Basic char array and cin query. gillianbc C++ Programming 4 November 4th, 2004 07:44 PM
Char array cin query (additional) gillianbc C++ Programming 3 October 29th, 2004 03:22 PM
Replace or remove the last char from the string sabu21s XSLT 12 January 8th, 2004 05:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.