Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
7 replies Latest Post - ‏2012-12-04T15:11:59Z by OurGuest
SystemAdmin
SystemAdmin
3180 Posts
ACCEPTED ANSWER

Pinned topic How to do string comparision by ignoring case-sensitive ?

‏2012-11-30T08:40:00Z |
can anyone help how to do string comparision by ignoring case-sensitive ?
Updated on 2012-12-04T15:11:59Z at 2012-12-04T15:11:59Z by OurGuest
  • Mathias Mamsch
    Mathias Mamsch
    1957 Posts
    ACCEPTED ANSWER

    Re: How to do string comparision by ignoring case-sensitive ?

    ‏2012-11-30T09:30:15Z  in response to SystemAdmin

    Well the easiest way is to convert both strings to lower or upper case:
     

    string s1 = "Hello"
    string s2 = "hEllo" 
     
    print (lower s1 == lower s2) "\n"
    

     


    Regards, Mathias

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Updated on 2014-01-06T14:24:16Z at 2014-01-06T14:24:16Z by iron-man
    • hell_se
      hell_se
      4 Posts
      ACCEPTED ANSWER

      Re: How to do string comparision by ignoring case-sensitive ?

      ‏2012-12-03T15:32:20Z  in response to Mathias Mamsch

      This works nicely for plain ASCII characters (A-Z, a-z), but unfortunately not for non-latin (ΓΔΘФЦЩ etc) and accented characters (ÁÅÄÖÜÔ etc).

      I have written a few simple scripts (toUpper, toLower and toTitle) which work as one would expect, as long as you are using codepage 1252. In case someone is interested:

      const int CAPITAL_LETTER_A = 0x41
      const int CAPITAL_LETTER_Z = 0x5a
      const int SMALL_LETTER_a = 0x61
      const int SMALL_LETTER_z = 0x7a
      const int CAPITAL_LETTER_A_WITH_GRAVE_ACCENT = 0xc0
      const int CAPITAL_ICELANDIC_LETTER_THORN = 0xde
      const int SMALL_LETTER_a_WITH_GRAVE_ACCENT = 0xe0
      const int SMALL_ICELANDIC_LETTER_THORN = 0xfe
      const int MULTIPLICATION_SIGN = 0xd7
      const int DIVISION_SIGN = 0xf7
      const int CAPITAL_LETTER_S_WITH_CARON = 0x8a
      const int SMALL_LETTER_s_WITH_CARON = 0x9a
      const int CAPITAL_DIGRAPH_OE = 0x8c
      const int SMALL_DIGRAPH_oe = 0x9c
      const int CAPITAL_LETTER_Z_WITH_CARON = 0x8e
      const int SMALL_LETTER_z_WITH_CARON = 0x9e
      const int CAPITAL_LETTER_Y_WITH_DIAERESIS = 0x9f
      const int SMALL_LETTER_y_WITH_DIAERESIS = 0xff
       
      bool whiteSpace (char c)
      {
          return(c==' ' || c=='\t' || c=='\r' || c=='\n' || c=='\v' || c=='\f')
      }
       
      string toUpper (string s)
      {
              int i,n
              string sUpper = null
              
              if (currentANSIcodepage != 1252) return s       // Unknown codepage...
       
              for (i = 0; i < length (s); i++)
              {
                      n = intOf (s[i])
                      if ((n >= SMALL_LETTER_a && n <= SMALL_LETTER_z) || (n >= SMALL_LETTER_a_WITH_GRAVE_ACCENT && n <= SMALL_ICELANDIC_LETTER_THORN && n != DIVISION_SIGN))
                              n = n - 0x20
                      elseif (n == SMALL_LETTER_s_WITH_CARON)
                              n = CAPITAL_LETTER_S_WITH_CARON
                      elseif (n == SMALL_DIGRAPH_oe)
                              n = CAPITAL_DIGRAPH_OE
                      elseif (n==SMALL_LETTER_z_WITH_CARON)
                              n = CAPITAL_LETTER_Z_WITH_CARON
                      elseif (n == SMALL_LETTER_y_WITH_DIAERESIS)
                              n = CAPITAL_LETTER_Y_WITH_DIAERESIS
                      sUpper = sUpper charOf (n) ""
              }
              return sUpper
      }
       
      string toLower (string s)
      {
              int i,n
              string sLower = null
              
              if (currentANSIcodepage != 1252) return s       // Unknown codepage...
       
              for (i = 0; i < length (s); i++)
              {
                      n = intOf (s[i])
                      if (( n >= CAPITAL_LETTER_A && n <= CAPITAL_LETTER_Z) || (n >= CAPITAL_LETTER_A_WITH_GRAVE_ACCENT && n <= CAPITAL_ICELANDIC_LETTER_THORN && n != MULTIPLICATION_SIGN))
                              n = n + 0x20
                      elseif (n == CAPITAL_LETTER_S_WITH_CARON)
                              n = SMALL_LETTER_s_WITH_CARON
                      elseif (n == CAPITAL_DIGRAPH_OE)
                              n = SMALL_DIGRAPH_oe
                      elseif (n == CAPITAL_LETTER_Z_WITH_CARON)
                              n = SMALL_LETTER_z_WITH_CARON
                      elseif (n == CAPITAL_LETTER_Y_WITH_DIAERESIS)
                              n = SMALL_LETTER_y_WITH_DIAERESIS
                      sLower = sLower charOf(n) ""
              }
              return sLower
      }
       
      string toTitle (string s)
      {
              int i,n
              string sTitle = null
              bool upper = true
              
              if (currentANSIcodepage != 1252) return s       // Unknown codepage...
       
              for (i = 0; i < length (s); i++)
              {
                      n = intOf (s[i])
                      if (whiteSpace (s[i]))
                              upper = true
                      else
                      {
                              if (upper)
                              {
                                      if ((n >= SMALL_LETTER_a && n <= SMALL_LETTER_z) || (n >= SMALL_LETTER_a_WITH_GRAVE_ACCENT && n <= SMALL_ICELANDIC_LETTER_THORN && n != DIVISION_SIGN))
                                              n = n - 0x20
                                      elseif (n == SMALL_LETTER_s_WITH_CARON)
                                              n = CAPITAL_LETTER_S_WITH_CARON
                                      elseif (n == SMALL_DIGRAPH_oe)
                                              n = CAPITAL_DIGRAPH_OE
                                      elseif (n == SMALL_LETTER_z_WITH_CARON)
                                              n=CAPITAL_LETTER_Z_WITH_CARON
                                      elseif (n == SMALL_LETTER_y_WITH_DIAERESIS)
                                              n = CAPITAL_LETTER_Y_WITH_DIAERESIS
                              }
                              elseif ((n >= CAPITAL_LETTER_A && n <= CAPITAL_LETTER_Z) || (n >= CAPITAL_LETTER_A_WITH_GRAVE_ACCENT && n <= CAPITAL_ICELANDIC_LETTER_THORN && n != MULTIPLICATION_SIGN))
                                      n = n + 0x20
                              elseif (n == CAPITAL_LETTER_S_WITH_CARON)
                                      n = SMALL_LETTER_s_WITH_CARON
                              elseif (n == CAPITAL_DIGRAPH_OE)
                                      n = SMALL_DIGRAPH_oe
                              elseif (n == CAPITAL_LETTER_Z_WITH_CARON)
                                      n = SMALL_LETTER_z_WITH_CARON
                              elseif (n == CAPITAL_LETTER_Y_WITH_DIAERESIS)
                                      n = SMALL_LETTER_y_WITH_DIAERESIS
                              upper = false
                      }
                      sTitle = sTitle charOf (n) ""
              }
              return sTitle
      }
      

       


      Regards, Lennart

       

      Updated on 2014-01-06T14:24:36Z at 2014-01-06T14:24:36Z by iron-man
  • OurGuest
    OurGuest
    163 Posts
    ACCEPTED ANSWER

    Re: How to do string comparision by ignoring case-sensitive ?

    ‏2012-12-03T16:34:05Z  in response to SystemAdmin
    int cistrcmp(string s1,string s2)
    • hell_se
      hell_se
      4 Posts
      ACCEPTED ANSWER

      Re: How to do string comparision by ignoring case-sensitive ?

      ‏2012-12-04T07:09:34Z  in response to OurGuest
      print cistrcmp ("qwérty", "QWÉRTY") // prints 1...
      • OurGuest
        OurGuest
        163 Posts
        ACCEPTED ANSWER

        Re: How to do string comparision by ignoring case-sensitive ?

        ‏2012-12-04T13:01:20Z  in response to hell_se
        Which is what one would expect: there is no reason to expect that é is the lower case of É
        • Mathias Mamsch
          Mathias Mamsch
          1957 Posts
          ACCEPTED ANSWER

          Re: How to do string comparision by ignoring case-sensitive ?

          ‏2012-12-04T14:40:48Z  in response to OurGuest

          The unicode consortium says otherwise, which has put a clear distinction into capital, small and title case letters. See also:

          http://unicode.org/faq/casemap_charprop.html

          Why would you think that there is NO reason to assume that the upper case of é is É? It would seem pretty natural, doesn't it?

          When you for example type in python:
           

          print u"abcdé".upper()
          


          you also get:

           

          "ABCDÉ"
          



          Of course when you type print "abcdé".upper() (without specifying a unicode string) then the é will not be upper cased, since you did not specify this. Since DOORS should naturally handle UTF-8 strings, I would conclude that this is indeed a bug. Regards, Mathias



           

           


          Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

           

          Updated on 2014-01-06T14:25:06Z at 2014-01-06T14:25:06Z by iron-man
          • OurGuest
            OurGuest
            163 Posts
            ACCEPTED ANSWER

            Re: How to do string comparision by ignoring case-sensitive ?

            ‏2012-12-04T15:11:59Z  in response to Mathias Mamsch
            If you want to make the comparison in python you are a shoe in.
            If you want to make the comparison in DOORS your are a shoe out.