Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. getting specific string error? [modified]

getting specific string error? [modified]

Scheduled Pinned Locked Moved C#
databasehelptutorialquestionworkspace
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maifs
    wrote on last edited by
    #1

    i want to fetch the characters from file for example : 1st example: // mehmood// hello ,how r u? just within comments and replace it to all caps 2nd example: /* hi mehmood ahmed */ i want to fetch the characters within sigle line comment and multi line comments . just within comments not outside the comment. -------------------------------------------------------------------------------- i am trying this code, kindly correct my code:

    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

            StreamReader sr = new StreamReader(fs);
            int countComment=0;
            while(sr.Peek() >= 0 )
            {
                str =sr.ReadLine();
                strTemp = new char\[str.Length\];
               #region From String method
                int count=0;
                for (int i = 0; i < str.Length; i++)
                {
                    if (str\[i\] == '/')
                    {
                        ++countComment;          
                        count=i;
                    }
                    if (char.IsLetterOrDigit(str,count+1) && countComment==2)
                    {
                        strTemp2 += str.Substring(count+1).ToUpper();                       
                    }                  
                    
                }
                #endregion
    
               
                countComment = 0;
                this.textBox2.Text += strTemp2 + Environment.NewLine; ;
                this.textBox1.Text += str + Environment.NewLine;
            }
            sr.Close();
            fs.Close();
    

    it generates an exception : System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index

    Maifs

    modified on Monday, July 13, 2009 5:08 PM

    L C 2 Replies Last reply
    0
    • M maifs

      i want to fetch the characters from file for example : 1st example: // mehmood// hello ,how r u? just within comments and replace it to all caps 2nd example: /* hi mehmood ahmed */ i want to fetch the characters within sigle line comment and multi line comments . just within comments not outside the comment. -------------------------------------------------------------------------------- i am trying this code, kindly correct my code:

      FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

              StreamReader sr = new StreamReader(fs);
              int countComment=0;
              while(sr.Peek() >= 0 )
              {
                  str =sr.ReadLine();
                  strTemp = new char\[str.Length\];
                 #region From String method
                  int count=0;
                  for (int i = 0; i < str.Length; i++)
                  {
                      if (str\[i\] == '/')
                      {
                          ++countComment;          
                          count=i;
                      }
                      if (char.IsLetterOrDigit(str,count+1) && countComment==2)
                      {
                          strTemp2 += str.Substring(count+1).ToUpper();                       
                      }                  
                      
                  }
                  #endregion
      
                 
                  countComment = 0;
                  this.textBox2.Text += strTemp2 + Environment.NewLine; ;
                  this.textBox1.Text += str + Environment.NewLine;
              }
              sr.Close();
              fs.Close();
      

      it generates an exception : System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index

      Maifs

      modified on Monday, July 13, 2009 5:08 PM

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Issues 1 - the code is a mess 2 - it won't work 3 - it's hard to read Do you not know how to use the debugger ? The error message is telling you what is wrong, what is the issue ?

      maifs wrote:

      if (char.IsLetterOrDigit(str,count+1) && countComment==2) { strTemp2 += str.Substring(count+1).ToUpper(); }

      Why do you use -1 AND +1 ? What if +1 is out of range ( wait, isn't that what the error message says ? ) This needs a lot of work. I'd read a C# book to learn some basics, then rewrite it using regex and the split method of the string class.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      1 Reply Last reply
      0
      • M maifs

        i want to fetch the characters from file for example : 1st example: // mehmood// hello ,how r u? just within comments and replace it to all caps 2nd example: /* hi mehmood ahmed */ i want to fetch the characters within sigle line comment and multi line comments . just within comments not outside the comment. -------------------------------------------------------------------------------- i am trying this code, kindly correct my code:

        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

                StreamReader sr = new StreamReader(fs);
                int countComment=0;
                while(sr.Peek() >= 0 )
                {
                    str =sr.ReadLine();
                    strTemp = new char\[str.Length\];
                   #region From String method
                    int count=0;
                    for (int i = 0; i < str.Length; i++)
                    {
                        if (str\[i\] == '/')
                        {
                            ++countComment;          
                            count=i;
                        }
                        if (char.IsLetterOrDigit(str,count+1) && countComment==2)
                        {
                            strTemp2 += str.Substring(count+1).ToUpper();                       
                        }                  
                        
                    }
                    #endregion
        
                   
                    countComment = 0;
                    this.textBox2.Text += strTemp2 + Environment.NewLine; ;
                    this.textBox1.Text += str + Environment.NewLine;
                }
                sr.Close();
                fs.Close();
        

        it generates an exception : System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index

        Maifs

        modified on Monday, July 13, 2009 5:08 PM

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, you need to debug your code, and you should do that yourself. Here are some hints: - an exception contains a message and a stack traceback indicating class and method names, file names, and line numbers (when available; use debug mode!) - when you try-catch an exception, make sure to show the ENTIRE exception, use Exception.ToString() - teach your IDE to always show line numbers in editor windows; for Visual Studio, look under menu Tools/Options/TextEditor/AllLanguages and if you must show us some code please use PRE tags (that's under the "code block" widget above the text edit box) :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups