tricky /n

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brendan

    tricky /n

    is /n counted as one character or two?
    ie I want to cut any line breaks from the begining of a message
    should it be
    while(substr($m essage,0,1)=="\ n") $message=substr ($message,1);
    or
    while(substr($m essage,0,3)=="\ n") $message=substr ($message,2);

    ???
  • Andy Hassall

    #2
    Re: tricky /n

    On Mon, 25 Oct 2004 13:24:00 +0100, brendan
    <brendan_nospam _@srl.cam.ac.uk _nospam> wrote:
    [color=blue]
    >is /n counted as one character or two?[/color]

    "/n" is two.
    '/n' is two.
    '\n' is two.
    "\n" is ONE - a newline character.
    [color=blue]
    >ie I want to cut any line breaks from the begining of a message
    >should it be
    >while(substr($ message,0,1)==" \n") $message=substr ($message,1);
    >or
    >while(substr($ message,0,3)==" \n") $message=substr ($message,2);[/color]

    As a further complication, the end of line in Windows /is/ two characters -
    "\r\n". Whereas on Unix, it's one, "\n". (I think on Macs it is just "\r").

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Michael Fesser

      #3
      Re: tricky /n

      .oO(brendan)
      [color=blue]
      >is /n counted as one character or two?[/color]

      Depends.
      [color=blue]
      >ie I want to cut any line breaks from the begining of a message
      >should it be
      >while(substr($ message,0,1)==" \n") $message=substr ($message,1);
      >or
      >while(substr($ message,0,3)==" \n") $message=substr ($message,2);[/color]

      trim() exists.

      Micha

      Comment

      • Pedro Graca

        #4
        Re: tricky /n

        brendan wrote:[color=blue]
        > is /n counted as one character or two?[/color]

        two characters, no matter how you delimit it

        "/n" == '/n'


        However, with a backslash instead, it is different:

        strlen("\n") == 1
        strlen('\n') == 2


        [color=blue]
        > ie I want to cut any line breaks from the begining of a message
        > should it be
        > while(substr($m essage,0,1)=="\ n") $message=substr ($message,1);
        > or
        > while(substr($m essage,0,3)=="\ n") $message=substr ($message,2);
        >
        > ???[/color]

        Neither. use ltrim()

        Strip whitespace (or other characters) from the beginning of a string

        --
        USENET would be a better place if everybody read: | to mail me: simply |
        http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
        http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
        http://www.expita.com/nomime.html | and *NO* attachments. |

        Comment

        Working...