9 Replies - 4747 Views - Last Post: 10 February 2010 - 06:06 AM Rate Topic: -----

#1 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Split string word into array

Posted 09 February 2010 - 09:42 AM

As the topic title says, I have a string (string word = "Word"), which I want to split up into a string array (string[]).

How do I do this?

The string is always just one word.
Is This A Good Question/Topic? 0
  • +

Replies To: Split string word into array

#2 RBSprogram101  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 28-March 09

Re: Split string word into array

Posted 09 February 2010 - 09:45 AM

ok so you are looking for string = "word" to become string array[4] = "w""o""r""d"?
Was This Post Helpful? 0
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Split string word into array

Posted 09 February 2010 - 09:45 AM

So you want to convert:

"Word" into:

["W","o","r","d"]?
Was This Post Helpful? 0
  • +
  • -

#4 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Re: Split string word into array

Posted 09 February 2010 - 09:46 AM

Yes :)
Was This Post Helpful? 0
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Split string word into array

Posted 09 February 2010 - 09:58 AM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string word = "Word";

            word.ToCharArray();

            foreach (char x in word)
            {
                Console.WriteLine(x);
            }

            Console.ReadLine();
        }
    }
}


You can read more information here if you'd like. :)
http://dotnetperls.com/string-split

This post has been edited by stapia.gutierrez: 09 February 2010 - 09:59 AM

Was This Post Helpful? 0
  • +
  • -

#6 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Split string word into array

Posted 09 February 2010 - 10:09 AM

I think that you would need an array of char, since you are splitting the string in characters, but anyway. Here is the code to convert the string to a string array.

string[] StringToArray(string source)
{
    string initString = source;
    char[] arrayOfChar = initString.ToCharArray();
    string[] arrayOfString = new string[arrayOfChar.Length];

    for(int i =0; i < arrayOfChar.Length-1; i++)
    {
       arrayOfString[i] = arrayOfChar[i].ToString();
    }

    return arrayOfString;
}



If you decide to get the char array, just remove the loop and change the returned array.
Was This Post Helpful? 1
  • +
  • -

#7 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Re: Split string word into array

Posted 09 February 2010 - 10:59 AM

Thanks guys
Was This Post Helpful? 0
  • +
  • -

#8 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Split string word into array

Posted 09 February 2010 - 01:33 PM

I read this a little differently, but I thought the OP was asking for a way to break a string into an array of strings. All these answers use a string of char. If I am wrong please ignore, if I am right then just use string.split The MSDN documentation is on the link below. Enjoy!

http://msdn.microsof...28VS.71%29.aspx
Was This Post Helpful? 1
  • +
  • -

#9 EvLSnoopY  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Re: Split string word into array

Posted 09 February 2010 - 06:24 PM

This is the way I would do it:
namespace MyExample
{
    public class Program
    {
        public void Run(string word)
        {
            // Instantiating a List object of type char.
            List<char> myList = new List<char>();

            try
            {
                for ( int i = 0; i < word.Length; i++ )
                {
                    myList.Add(word[i]);
                    Console.WriteLine(myList[i]);
                }
            }
            catch
            {
                // Catch statement just in case.
            }
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            p.Run("Word to ya' motha!");
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Split string word into array

Posted 10 February 2010 - 06:06 AM

View PostAdkins, on 09 February 2010 - 03:33 PM, said:

I read this a little differently, but I thought the OP was asking for a way to break a string into an array of strings. All these answers use a string of char. If I am wrong please ignore, if I am right then just use string.split The MSDN documentation is on the link below. Enjoy!

http://msdn.microsof...28VS.71%29.aspx


I am not sure why Adkins got a negative vote for this post since the OP did ask for a string array. I have added an up vote to even it out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1