Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

load string into char[] array c#

Posted on 2011-09-20
6
Medium Priority
?
544 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 80 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 120 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 120 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 8

Accepted Solution

by:
jagrut_patel earned 1600 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 80 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 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