Skip to main content
Engineering LibreTexts

11.8: Character Comparisons

  • Page ID
    54311
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The standard relational operators (“==”, “>”, “>=”, etc.) have some limitations when character data is used. Simple comparisons, such as,

    "A" < "D"
    "ABC" == "ABC"
    

    will work as expected. That is, both will evaluate to true.

    However, when comparing, the following characters, each will evaluate to false.

    "A" > "a"
    "20" < "100"
    "ABCD" <= "ABC"
    

    This is a result of the relational operations referring to the assigned values (based on their location in the ASCII table located in Appendix A).

    Comparisons between digits, “0” - “9”, will work relative to each other. Comparisons between upper- case letters, “A” - “Z”, will also work relative to each other. Comparisons between lower-case letters, “a” - “z”, will also work relative to each other. Since the lower case letters are after the upper case letters in the table an upper-case letter will be less than a lower-case letter. The digits are in the table before the letters (upper- and lower-case), so they will evaluate as less than letters. This must be taken into account when dealing with character comparisons.


    This page titled 11.8: Character Comparisons is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.