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. Newbie! Search a string for a specific character?

Newbie! Search a string for a specific character?

Scheduled Pinned Locked Moved C#
question
6 Posts 4 Posters 1 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.
  • H Offline
    H Offline
    hristo1977
    wrote on last edited by
    #1

    Hi! This dont seem to work, any ideas? (Im sure you have lots of ideas, but remember Im a newbie) label1.Text += "."; string k = label1.Text; int r; r = label1.Text.IndexOf(k); if (r > 0) { MessageBox.Show("bla"); } so, r is never above 0 despite that the string contains a "."

    Newbie untill I die! :-)

    R 1 Reply Last reply
    0
    • H hristo1977

      Hi! This dont seem to work, any ideas? (Im sure you have lots of ideas, but remember Im a newbie) label1.Text += "."; string k = label1.Text; int r; r = label1.Text.IndexOf(k); if (r > 0) { MessageBox.Show("bla"); } so, r is never above 0 despite that the string contains a "."

      Newbie untill I die! :-)

      R Offline
      R Offline
      Ranjan Banerji
      wrote on last edited by
      #2

      IndexOf returns a 0 based index. "." is not in position 1, it is in position 0. If nothing is found -1 is returned. Your code should be if( r != -1 )

      My pointless rants meragussa.blogspot.com[^]

      H 1 Reply Last reply
      0
      • R Ranjan Banerji

        IndexOf returns a 0 based index. "." is not in position 1, it is in position 0. If nothing is found -1 is returned. Your code should be if( r != -1 )

        My pointless rants meragussa.blogspot.com[^]

        H Offline
        H Offline
        hristo1977
        wrote on last edited by
        #3

        hi, and thanks for your fast answer. Im trying to program a calculator and you should not be able to hit "." multiple times in a row. So if I take this code: string k = label1.Text; int r; r = label1.Text.IndexOf(k); if (r != -1) { label1.Text += "."; MessageBox.Show("bla"); } And paste in under, for example, the button 9, I still get the msgbox! That should not happend, shouild it? Because now, there are no "." in the string...or Im missing someting here?

        Newbie untill I die! :-)

        L J 2 Replies Last reply
        0
        • H hristo1977

          hi, and thanks for your fast answer. Im trying to program a calculator and you should not be able to hit "." multiple times in a row. So if I take this code: string k = label1.Text; int r; r = label1.Text.IndexOf(k); if (r != -1) { label1.Text += "."; MessageBox.Show("bla"); } And paste in under, for example, the button 9, I still get the msgbox! That should not happend, shouild it? Because now, there are no "." in the string...or Im missing someting here?

          Newbie untill I die! :-)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          hristo1977 wrote:

          string k = label1.Text; int r; r = label1.Text.IndexOf(k);

          Wait, you take the contents of your textbox (k) and test (IndexOf(k)), if it exists? This will always return the index 0. What you probably want is IndexOf(".") regards

          1 Reply Last reply
          0
          • H hristo1977

            hi, and thanks for your fast answer. Im trying to program a calculator and you should not be able to hit "." multiple times in a row. So if I take this code: string k = label1.Text; int r; r = label1.Text.IndexOf(k); if (r != -1) { label1.Text += "."; MessageBox.Show("bla"); } And paste in under, for example, the button 9, I still get the msgbox! That should not happend, shouild it? Because now, there are no "." in the string...or Im missing someting here?

            Newbie untill I die! :-)

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            hristo1977 wrote:

            Im trying to program a calculator and you should not be able to hit "."

            Here's a simple way to determine if the period character exists multiple times in a string:

            string text = label1.Text;
            int periodCount = 0;
            foreach(char character in text)
            {
            if(character == '.')
            {
            periodCount++;
            }
            }

            if(periodCount > 1)
            {
            // There are multiple period characters in the string.
            }

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: For Christians: The Significance of Yom Teruah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            H 1 Reply Last reply
            0
            • J Judah Gabriel Himango

              hristo1977 wrote:

              Im trying to program a calculator and you should not be able to hit "."

              Here's a simple way to determine if the period character exists multiple times in a string:

              string text = label1.Text;
              int periodCount = 0;
              foreach(char character in text)
              {
              if(character == '.')
              {
              periodCount++;
              }
              }

              if(periodCount > 1)
              {
              // There are multiple period characters in the string.
              }

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: For Christians: The Significance of Yom Teruah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              H Offline
              H Offline
              hristo1977
              wrote on last edited by
              #6

              Thanks a lot, Himango!

              Newbie untill I die! :-)

              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