Solved

load string into char[] array c#

Posted on 2011-09-20
6
503 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
Comment Utility
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
Comment Utility
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
Comment Utility
...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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 8

Accepted Solution

by:
jagrut_patel earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now