Skip navigation
Currently Being Moderated

Dealing with Special Characters.

Jan 5, 2011 8:39 AM

What is the syntax to check if a character is a certain special character?

 

...if(myCharacter.contents!=SpecialCharacters.INDENT_HERE_TAB)

{

.

.

.

}

 
Replies
  • Currently Being Moderated
    Jan 5, 2011 9:19 AM   in reply to pkrk

    If you try that, you'll find that the answer is "Yes".

    indentHereTab works, too.

     

    Peter

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 9:13 AM   in reply to pkrk

    That should work (although you'd want the == operator if your are testing for its existence...)

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 9:13 AM   in reply to Harbs.

    Peter got there first...

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 10:15 AM   in reply to pkrk

    end of paragraph is "\r".

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 2:21 PM   in reply to pkrk

    1) Also, it's important to remember that any SpecialCharacters member is implemented as a pure Number:

     

    'number' == typeof SpecialCharacters.INDENT_HERE_TAB;  // true

     

    And, in this case:

     

    1397319796===SpecialCharacters.INDENT_HERE_TAB;  // true

     

    So there is a potential issue in just using == or != when dealing with text contents. Suppose that a textual object (story, textframe, line, word, etc.) contains the string "1397319796". Then the test:

     

    myText.contents == SpecialCharacters.INDENT_HERE_TAB;

     

    will return true, which is not exactly what you expected! Indeed, JavaScript considers that:

     

    "1397319796" == 1397319796;

     

    So, when you compare a contents property and a SpecialCharacters member, it's recommended to use a triple-equal sign (===), or the opposite !==, to prevent any surprising conclusion.

     

    // Here we are sure that .contents is an actual IndentHereTab

    myText.contents === SpecialCharacters.INDENT_HERE_TAB;

     

    (Note that myText.contents, here, is a Number, not a String.)

     

    2) Another dangerous issue is that every SpecialCharacters member is silently converted into a String object (a simple character) when myText.contents contains *other* characters. For example, if myStory contains "A" followed by an IndentHereTab, then myStory.contents returns a string of two characters:

     

    myStory.contents === "A\x07";

     

    because SpecialCharacters.INDENT_HERE_TAB is internally translated into U+0007.

     

    But of course, you still have:

     

    myStory.characters[1].contents === 1397319796; // true

     

    and not:

     

    myStory.characters[1].contents === "\x07"; // false

    myStory.characters[1].contents == "\x07"; // false

     

    See also:

    http://www.indiscripts.com/post/2009/07/idcs4-special-characters

     

     

    @+

    Marc

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 2:39 PM   in reply to Marc Autret

    Hey Marc,

     

    While what you say is technically true, I don't think that this case is really a problem. If you are testing for a special character, the test has to be done on a single Character (or it'll fail for sure!) which can't contain more than a single character, so the test can't really fail for the reasons you say...

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 3:00 PM   in reply to Harbs.

    That's right, Harbs. My post was just a passing remark. I had so many problems with special characters when I started scripting, I just wanted to share.

     

    @+

    Marc

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 11:13 PM   in reply to Marc Autret

    Sure thing. I just wanted to clarify the point for anyone who might be reading this...

     

    Harbs

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points