2 Replies - 9306 Views - Last Post: 25 September 2011 - 11:19 PM Rate Topic: -----

#1 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Get a String after a specific Character (Character = ":")

Posted 25 September 2011 - 03:22 AM

Hi,

I have a problem/question.

I have this function:
    StreamReader ModStream;
          String modName = null;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            ofd.Filter = "Mod Files (*.mmi)|*.mmi";
            ofd.FilterIndex = 2;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String input;

                try
                {using (ModStream = new StreamReader(ofd.FileName)){
                    while ((input = ModStream.ReadToEnd()) != null){
                if(input == "Name:"){
                    
        
                        }
                }

             }
                }catch (Exception ex)
                {
                    MessageBox.Show("Error reading" + ofd.FileName + " Try contacting suport proudtobemexp@gmail.com");

                    Error_Class.error = ex.ToString();



                }

            
                

            }


now i need to get what's after the : of "Name:" Like "Name: File" and then there is a variable(modName)
that will store the String after the : of Name.
That's store's the name.

The name is stored inside a .mmi file.

And the syntax should be similar like this:
Name: MMIMod'
Desc: This is a test



Then the program will load the .mmi file and read it.

Now the problem is that im new to C#.

And that i don't now how todo it.:(.

If seen indexof ?

of something like substring?


Can anybody clear that out?


Thanks,

Arno

(Sorry for my english.)

This post has been edited by cornetto456: 25 September 2011 - 03:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Get a String after a specific Character (Character = ":")

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Get a String after a specific Character (Character = ":")

Posted 25 September 2011 - 03:29 AM

it's a combination of IndexOf and Substring.
string whatever = variable.Substring(variable.IndexOf(":") + 1, <count>)


This post has been edited by Ionut: 25 September 2011 - 03:30 AM

Was This Post Helpful? 2
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1408
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: Get a String after a specific Character (Character = ":")

Posted 25 September 2011 - 11:19 PM

I would say you should use the Split(char) method of the string class.

string myString = "Name: my name";
string[] tokens = myString.Split(':');
Console.WriteLine(tokens[1]);


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1