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. Help: checking a variable against a string ignoring case senstivity.

Help: checking a variable against a string ignoring case senstivity.

Scheduled Pinned Locked Moved C#
visual-studiohelptutorialquestion
5 Posts 4 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.
  • J Offline
    J Offline
    jblouir
    wrote on last edited by
    #1

    This is giving me a real headache, I hope someone out there can help me. :-) Basically what I have is a text based adventure that takes input from the user. for example... string input; Console.WriteLine("Where do you want to go?"); input = Console.ReadLine(); if ((input = "north") || (input = "North") || (input = "n") || (input = "N") { if ((y - 1) < 0) Console.WriteLine(You can't move any farther North"); else y = y - 1; } the bit that I want to change is where I check the input vs 4 values, north, North, n, and N. Is there any shorter way of doing this, say checking it for letters and ignoring case sensitivity. I suppose I could trim off the first letter, but then people could put anything in then like, nowhere, and they end up going north. Thanks for your help ahead of time.

    L H 2 Replies Last reply
    0
    • J jblouir

      This is giving me a real headache, I hope someone out there can help me. :-) Basically what I have is a text based adventure that takes input from the user. for example... string input; Console.WriteLine("Where do you want to go?"); input = Console.ReadLine(); if ((input = "north") || (input = "North") || (input = "n") || (input = "N") { if ((y - 1) < 0) Console.WriteLine(You can't move any farther North"); else y = y - 1; } the bit that I want to change is where I check the input vs 4 values, north, North, n, and N. Is there any shorter way of doing this, say checking it for letters and ignoring case sensitivity. I suppose I could trim off the first letter, but then people could put anything in then like, nowhere, and they end up going north. Thanks for your help ahead of time.

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

      hi, you could insert input=input.ToLower(); and forget about uppercase. :)

      Luc Pattyn [My Articles] [Forum Guidelines]

      J 1 Reply Last reply
      0
      • L Luc Pattyn

        hi, you could insert input=input.ToLower(); and forget about uppercase. :)

        Luc Pattyn [My Articles] [Forum Guidelines]

        J Offline
        J Offline
        jblouir
        wrote on last edited by
        #3

        Thanks a bunch, that helps loads. =)

        B 1 Reply Last reply
        0
        • J jblouir

          This is giving me a real headache, I hope someone out there can help me. :-) Basically what I have is a text based adventure that takes input from the user. for example... string input; Console.WriteLine("Where do you want to go?"); input = Console.ReadLine(); if ((input = "north") || (input = "North") || (input = "n") || (input = "N") { if ((y - 1) < 0) Console.WriteLine(You can't move any farther North"); else y = y - 1; } the bit that I want to change is where I check the input vs 4 values, north, North, n, and N. Is there any shorter way of doing this, say checking it for letters and ignoring case sensitivity. I suppose I could trim off the first letter, but then people could put anything in then like, nowhere, and they end up going north. Thanks for your help ahead of time.

          H Offline
          H Offline
          Hesham Yassin
          wrote on last edited by
          #4

          you can turn the whole position of the string to upper or lower case: String str = "aBcD"; str = str.ToUpper(); if(str == "ABCD") { // Bla Bla Bla } when the IP reaches the IF check, it alwayse will enter to // Bla Bla Bla:~

          1 Reply Last reply
          0
          • J jblouir

            Thanks a bunch, that helps loads. =)

            B Offline
            B Offline
            Brady Kelly
            wrote on last edited by
            #5

            Another option is String.Compare(s1, s2, true), where the true indicates to ignore case.

            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