C# - How to Find a "#" Character in a String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • str1ker
    New Member
    • Jun 2006
    • 23

    C# - How to Find a "#" Character in a String

    Hi,

    This has been driving me completely crazy, and I'm haunted by it day and night, and I'm sure that there's a simple answer that'll make me bash my head on a wall, but I have not found that answer anyway.

    So, I ask now, is there some escape sequence, or some method of finding the # character in a string?

    I've also had minor issues with other characters in other situations, like a tab next to a couple of spaces, stuff like that, but all I want to know now is what represents the "#" character in strings.

    Thanks,

    Ashton

    P.S. Again, sorry if it's so easy that it's stupid. I'm not a n00b programmer, proof is my <link removed> project, but seriously, this is driving me crazy.

    EDIT by Mod:
    We don't need proof, we help n00bs as well as advanced programmers
    Last edited by Curtis Rutland; Aug 11 '08, 06:30 PM. Reason: Removed Link
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    The # character represents the # character...jus t like a J represents a J....?
    I'm not sure what you are having trouble with?
    string.IndexOf( "#") should tell you where in the string the first "#" resides?
    [code=c#]
    string test="dan likes the #14 best";
    int n = test.IndexOf("# ");
    if (n != -1)
    {
    MessageBox.Show ("Found at: " + n.ToString());
    }
    [/code]

    Comment

    • nateraaaa
      Recognized Expert Contributor
      • May 2007
      • 664

      #3
      Please post the string you are trying to parse for the # character. Also any code that you have tried so far.

      thanks
      Nathan

      Comment

      • Dragas
        New Member
        • Aug 2008
        • 4

        #4
        If understanded u well u can do like this:

        string example = "1234#56890 ";
        bool yesno = false;

        for(int i=0; i<example.Lengt h; i++)
        {
        if(substring(i, 1) == "#") yesno = true;
        }

        So if # exist in string yesno will be true or u can pick up i as pointer where
        in string it is ....

        Also if u wana cast out spaces from string u can use
        for(int i=0; i<example.Trim( ).Length; i++) instead

        Hope will help if understanded you wath you need

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Code:
          string s = "abcde#fg";
          int index = s.IndexOf("#");
          You're doing it the hard way. IndexOf returns the first instance of the search pattern. If it doesn't find it, it returns -1.

          To trim:
          Code:
          string s = "   something here   ";
          s = s.Trim();
          //now s = "something here"

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            You can also use .TrimEnd() and .TrimStart()

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Also, strings work as literal character arrays. No need for the substring()
              So:
              [code=c#]
              string fred="jon's new car";
              if(fred[4]=='s')
              {//whatever you want
              }
              [/code]

              Comment

              • str1ker
                New Member
                • Jun 2006
                • 23

                #8
                Thanks for the replies.

                Well, I've got this syntax richtextbox, one that does colour-coding to make it easier to read code, shader code specifically, HLSL, and there are two keywords, "#include" and "#define" that I want to highlight.

                Trouble is, when I add either of those as a keyword:
                Code:
                syntaxRichTextBox1.Settings.Keywords.Add("#include");
                syntaxRichTextBox1.Settings.Keywords.Add("#define");
                It doesn't highlight those keywords at all. I've noticed that I get similar inabilities to find # characters in not-so-fancy applications I've developed.

                The SyntaxRichTextB ox I found here:


                I can settle for just highlighting "include" and "define" by themselves, it's only a bit annoying that # doesn't work.

                Thanks,

                Ashton

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  That sounds like an implementation failure on the part of whoever made the custom RichTextBox you are using, and not a fault of string character indexing.

                  Comment

                  • str1ker
                    New Member
                    • Jun 2006
                    • 23

                    #10
                    Originally posted by Plater
                    That sounds like an implementation failure on the part of whoever made the custom RichTextBox you are using, and not a fault of string character indexing.
                    Yeah, I've been looking through the code more.

                    There some fancy .NET functions to seek certain character sequences being used. I've tried changing a few settings, but #s are still not found. I forgot the name of the function, so I'll reply back when I find it again.

                    Something to do with System.Text.Reg ularExpressions I think.

                    Comment

                    Working...