Arthur Mamou-Mani
Arthur Mamou-Mani

Reputation: 2333

C#: Using the String.Split method to split a list of string at each separate line?

I am trying to split my list of strings at each separate lines using the String.Split method but the both the method below and a regex method did not work. Instead, they returned the following result {0} 0. System.String[] instead of the actual array of strings. Please help to find the error(s) in the code below:

    string m_settings_temp;
    string[] m_settings;
    public void ShowSettingsGui() {
       var dialog = new OpenFileDialog { Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" };
       if (dialog.ShowDialog() != DialogResult.OK) return;
       m_settings_temp = File.ReadAllText(dialog.FileName);
       m_settings = m_settings_temp.Split(new [] { '\r', '\n' });
       //This regex method failed as well:  
       //m_settings = Regex.Split(m_settings_temp,"\r\n|\r|\n");
    }

    //The method below is to evaluate the output
    protected override void SolveInstance(IGH_DataAccess DA)
                    {
                        if (m_settings == null)
                        {
                            AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
                            return;
                        }
                        DA.SetData(0, m_settings);
                    }

Thanks in advance!

Upvotes: 2

Views: 772

Answers (3)

Jodrell
Jodrell

Reputation: 35726

just use ReadAllLines like this

m_settings = File.ReadAllLines(dialog.FileName);

this will give you a string[] with an element for each line in the selected file. If, after running this code, m_settings has no elements, the file you selected was empty.


If I wanted to interrgoate the contents of m_settings in a console app I might do somthing like.

for (var i = 0; i < m_settings.Length; i ++)
{
    Console.WriteLine(m_settings[i]);
}

This would output the content of array, one element at a time. If I used the implementation of ToString for am array, like this,

Console.WriterLine(m_settings);

I would get a string representation of the array's type and the number of elements it contains.


I suspect in your case you want to do somthing like

protected override void SolveInstance(IGH_DataAccess DA)
{
     if (m_settings == null || m_settings.Length == 0)
     {
         AddRuntimeMessage(
              GH_RuntimeMessageLevel.Warning,
              "You must declare some valid settings");

         return;
     }

     for (var i = 0; i < m_settings.Length; i ++)
     {
         DA.SetData(i, m_settings[i]);
     }
}

Upvotes: 7

pjvds
pjvds

Reputation: 956

string m_settings_temp;
string[] m_settings;
public void ShowSettingsGui()
{
    var dialog = new OpenFileDialog { Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" };
    if (dialog.ShowDialog() != DialogResult.OK) return;

    m_settings_temp = File.ReadAllText(dialog.FileName);
    m_settings = m_settings_temp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
}

Upvotes: 1

Dennis Traub
Dennis Traub

Reputation: 51654

m_settings = m_settings_temp
    .Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

Upvotes: 1

Related Questions