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.
Split string word into array
Page 1 of 19 Replies - 3560 Views - Last Post: 10 February 2010 - 06:06 AM
Replies To: Split string word into array
#2
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"?
#3
Re: Split string word into array
Posted 09 February 2010 - 09:45 AM
So you want to convert:
"Word" into:
["W","o","r","d"]?
"Word" into:
["W","o","r","d"]?
#5
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
#6
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.
If you decide to get the char array, just remove the loop and change the returned 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.
#8
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
http://msdn.microsof...28VS.71%29.aspx
#9
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!");
}
}
}
#10
Re: Split string word into array
Posted 10 February 2010 - 06:06 AM
Adkins, 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
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|