Solved

load string into char[] array c#

Posted on 2011-09-20
6
529 Views
Last Modified: 2013-12-17
I am trying to load a string

test1str  ' '; ','; '.'; ':'; '\t';'|';'='
into a char array, the seperator is the semi colon,

so that I can some how assign the char array with the same result as if i had done

char[] CharArray = { ' ', ',', '.', ':', '\t','|','='};

this is so i can create a mehtod like
        public char[] PopulateCharArrayFromString(string StringToLoad, string StringSeperator)
        { ...}

Iam using visural studio 2010 .net4 framework

thanks again for the help
0
Comment
Question by:jhacharya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Assisted Solution

by:x77
x77 earned 20 total points
ID: 36567544
String has Constructor from Char Array:

   public String(char[] value,      int startIndex,      int length)

You can Find separator with  

     Array.IndexOf<char>(a, ';');

0
 
LVL 5

Assisted Solution

by:Kelaros
Kelaros earned 30 total points
ID: 36567562
You can just iterate through your string like an array, accessing each character through StringToLoad[x] or whatever your loop variable is.  If StringToLoad[x] is == StringSeperator, skip it and go to the next one.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 30 total points
ID: 36567921
...and what if the separator, semi-colon in this case, is actually one of the values?  Note that the values are enclosed in single quotes, so it would look like this:

    'a';';';'c'
 
The middle semi-colon should be treated as one of the values, not a separator.  

Same problem occurs if the single quote should actually be one of the values:

    'a';''';'c'

Also, the "\t" would be a literal two character string so you'd have to use char.Parse() to convert that to a char.

I don't think any kind of straight Split(), Replace(), or IndexOf() will solve this problem.  It'd have to be a little more intelligent than that using an algorithm or possibly with a Regular Expression (I suck at those).


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Accepted Solution

by:
jagrut_patel earned 400 total points
ID: 36571746
Try this,

string test1str = "' '; ','; '.'; ':'; '\t';'|';'=';'''";
string[] a1 = test1str.Split(';');
string[] a2 = new string[a1.Length];
for (int i = 0; i < a1.Length; i++)
{
    a2[i] = a1[i].Split('\'')[1];
    if(string.IsNullOrEmpty(a2[i]))
        a2[i] = "'";
}
char[] b1 = new char[a2.Length];
for(int i=0;i<a2.Length;i++)
    b1[i] = Convert.ToChar(a2[i]);

//b1 char array contains the result

Open in new window


HTH!
0
 
LVL 13

Assisted Solution

by:jonnidip
jonnidip earned 20 total points
ID: 36572077
You can take a look to this project, that treats your same question: http://www.codeproject.com/KB/dotnet/TextQualifyingSplit.aspx

Regards.
0
 
LVL 2

Author Closing Comment

by:jhacharya
ID: 36580731
While everyone did have input and has mede me look at the problem in slightly different ways the best solution was jagrut, as this actually solved my issue.

here is the method i have created

        public char[] PopulateSplitString(string StringToSplit, char SplitChar)
        {
            string test1str = "";
            if (StringToSplit == null)
            { test1str = "' '; ','; '.'; ':'; '\t';'|';'=';'''"; }
            else
            { test1str = StringToSplit; }
            string[] a1 = test1str.Split(SplitChar);
            string[] a2 = new string[a1.Length];
            for (int i = 0; i < a1.Length; i++)
            {
                a2[i] = a1[i].Split('\'')[1];
                if (string.IsNullOrEmpty(a2[i]))
                    a2[i] = "'";
            }
            char[] b1 = new char[a2.Length];
            for (int i = 0; i < a2.Length; i++)
            {
                b1[i] = Convert.ToChar(a2[i]);
            }

            return b1;
        } //char[] PopulateSplitString(string StringToSplit, char SplitChar)

thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question