4 Replies - 13175 Views - Last Post: 24 August 2008 - 09:06 PM Rate Topic: -----

#1 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Converting String[] to Char[]

Posted 24 August 2008 - 06:24 PM

I have searched google and have found how to convert a string to char[]. That's not what I want I want to convert String[] to Char[]. Is that possible, if so can you give me a nudge in the right direction.

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Converting String[] to Char[]

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Converting String[] to Char[]

Posted 24 August 2008 - 07:08 PM

To convert a char array to a string is actually quiet easy. There's an overload to the string that accepts a char array, like

char[] str = {'a', 'b', 'c', 'd'};
string converted = new string(str);



That will convert a char array to a string is just as easy

string arry = "hello world";
char[] strConverted = arry.ToCharArray(0, arry.Length);


Was This Post Helpful? 0
  • +
  • -

#3 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Converting String[] to Char[]

Posted 24 August 2008 - 07:36 PM

View PostPsychoCoder, on 24 Aug, 2008 - 07:08 PM, said:

To convert a char array to a string is actually quiet easy. There's an overload to the string that accepts a char array, like

char[] str = {'a', 'b', 'c', 'd'};
string converted = new string(str);



That will convert a char array to a string is just as easy

string arry = "hello world";
char[] strConverted = arry.ToCharArray(0, arry.Length);



Sorry that's not what I meant. I meant converting a String array to a Char Array.
Was This Post Helpful? 0
  • +
  • -

#4 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Converting String[] to Char[]

Posted 24 August 2008 - 08:55 PM

Wow, psycho, i can't believe you made a mistake like that... are you sick???

I am not quite clean on what you want, but i think you are wanting to have an array of strings, and merge each string in that collection into one string, and convert that string into an array of chars.

Anyway, here is something that should do what you want
        private char[] StrToChr(string[] Strings)
        {
            //merges the items in the Strings collection into one string
            string merged = "";
            foreach (string str in Strings)
            {
                merged += str;
            }
            //returns a char array that represents the merged string
            return merged.ToCharArray();
        }


Be warned, i have not tested it.
Ok, i just tested it, and it works fine!

This post has been edited by jacobjordan: 24 August 2008 - 09:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Converting String[] to Char[]

Posted 24 August 2008 - 09:06 PM

View Postjacobjordan, on 24 Aug, 2008 - 08:55 PM, said:

Wow, psycho, i can't believe you made a mistake like that... are you sick???

I am not quite clean on what you want, but i think you are wanting to have an array of strings, and merge each string in that collection into one string, and convert that string into an array of chars.

Anyway, here is something that should do what you want
        private char[] StrToChr(string[] Strings)
        {
            //merges the items in the Strings collection into one string
            string merged = "";
            foreach (string str in Strings)
            {
                merged += str;
            }
            //returns a char array that represents the merged string
            return merged.ToCharArray();
        }


Be warned, i have not tested it.
Ok, i just tested it, and it works fine!



Ok thanks jacob
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1