Thread: mystring.split help

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    10

    mystring.split help

    Code:
    Ok as you might of guess from the title i am trying to split a string down to an array with a new 'Ray' ( not sure what the actural tecinal term is for a single line in an array) give every time '{' Appears
    
    this is the script im using 
    Code:
    string[] aryRacelist = strRace.Split('}');
    But im getting an error saying system.IO.Streamreader does not contain a definition for 'Split' So i believe the error lie in i have not reference the proper using System.whatever that contains Split. Can anyone tell me what it is, or have i just gone really wrong. i believe my coding right because i copied it off online example. Anyhelp would be nice
    sry that it all in code bracket, it wouldn't let me post without

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Looks like strRace is a StreamReader and not a string. Usually you would do something like:
    Code:
    using (StreamReader sr = new StreamReader(streamYouWantToRead))
    {
        string line;
        while (null != (line = sr.ReadLine()))
        {
            string[] splitString = line.Split('}');
        }
    }

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Or even simplier:
    Code:
    strRace.ReadToEnd().Split(new char[] { '}' } );
    If you dislike empty lines (like you get if you have "} }"), use this:
    Code:
    strRace.ReadToEnd().Split(new char[] { '}' }, System.StringSplitOptions.RemoveEmptyEntries);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    10
    Ok i copied the code as best i could understand but it doesn't work, also i don't understand, what is the name of the array generated

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                StreamReader strRace = new StreamReader("Z:\\Homeworld2\\scripts\\race.lua");
                txtCurrent.Text = strRace.ReadToEnd();
                strRace.ReadToEnd().Split(new char[] { '{' });
                lbxRaces.Items.AddRange();   
                strRace.Close();
                
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
        }
    }
    and here my error code

    Code:
    ------ Build started: Project: WindowsApplication2, Configuration: Debug Any CPU ------
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /debug+ /debug:full /optimize- /out:"obj\Debug\Kaos Project.exe" /resource:obj\Debug\WindowsApplication2.Form1.resources /resource:obj\Debug\KaosProject.Properties.Resources.resources /target:winexe Form1.cs Form1.Designer.cs Program.cs Properties\AssemblyInfo.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs
    C:\Documents and Settings\Dave Wilson\My Documents\Visual Studio 2005\Projects\WindowsApplication2\WindowsApplication2\Form1.cs(24,13): error CS1501: No overload for method 'AddRange' takes '0' arguments
    c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll: (Related file)
    
    Compile complete -- 1 errors, 0 warnings
    ========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The error message is very clear. The AddRange method of the ListBox class requires an argument.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Code:
                txtCurrent.Text = strRace.ReadToEnd();
                strRace.ReadToEnd().Split(new char[] { '{' });
    Don't ReadToEnd twice. Use txtCurrent.Text as source in your second line.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    10
    Quote Originally Posted by nvoigt View Post
    Code:
                txtCurrent.Text = strRace.ReadToEnd();
                strRace.ReadToEnd().Split(new char[] { '{' });
    Don't ReadToEnd twice. Use txtCurrent.Text as source in your second line.
    ok but what is the name of the array generated??

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The name is whatever you want it to be, you just assign the result of the split to a string array variable!
    Code:
    string [] races = strRace.ReadToEnd().Split(new char[] { '{' });
    lbxRaces.Items.AddRance(races);

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    10
    ok one last thing, when i try load a string from the list i get this

    Code:
    string strRacedata = new string(lbxRaces.SelectedItem.ToString());
    Code:
    ------ Build started: Project: WindowsApplication2, Configuration: Debug Any CPU ------
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /debug+ /debug:full /optimize- /out:"obj\Debug\Kaos Project.exe" /resource:obj\Debug\WindowsApplication2.Form1.resources /resource:obj\Debug\KaosProject.Properties.Resources.resources /target:winexe Form1.cs Form1.Designer.cs Program.cs Properties\AssemblyInfo.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs
    C:\Documents and Settings\Dave Wilson\My Documents\Visual Studio 2005\Projects\WindowsApplication2\WindowsApplication2\Form1.cs(35,32): error CS1502: The best overloaded method match for 'string.String(char*)' has some invalid arguments
    C:\Documents and Settings\Dave Wilson\My Documents\Visual Studio 2005\Projects\WindowsApplication2\WindowsApplication2\Form1.cs(35,43): error CS1503: Argument '1': cannot convert from 'string' to 'char*'

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You don't need to use new string, just get the one that's selected.
    Code:
    string strRacedata = lbxRaces.SelectedItem.ToString();

Popular pages Recent additions subscribe to a feed