Solved

load string into char[] array c#

Posted on 2011-09-20
6
522 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
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 85

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 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