Solved

load string into char[] array c#

Posted on 2011-09-20
6
523 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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