modify string in one position only

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

    #1

    modify string in one position only

    Is it possible to modify a string in one place using an indexing operator
    and an assignment statement?

    E.g.:
    $s = "1234567890 ";
    $s[5] = "d"; // $s is now "12345d7890 "

    According to _PHP Bible_ (2nd ed., p.175), this kind of syntax can work ,
    but is undocumented and, moreover, appears to be discouraged because "almost
    all PHP string manipulation functions return modified copies of their string
    arguments rather than making direct changes, which seems to indicate that
    this is the style that the PHP designers prefer."

    I did a google search (both www and USENET) and found nothing on this.


  • Jon Kraft

    #2
    Re: modify string in one position only

    "sinister" <sinister@nospa m.invalid> wrote:
    [color=blue]
    > Is it possible to modify a string in one place using an indexing
    > operator and an assignment statement?
    >
    > E.g.:
    > $s = "1234567890 ";
    > $s[5] = "d"; // $s is now "12345d7890 "[/color]

    Hi,

    Square brackets here are deprecated since 4.0, use curly braces:

    $s = "1234567890 ";
    $s{5} = "d"; // $s is now "12345d7890 "

    HTH;
    JOn

    Comment

    • sinister

      #3
      Re: modify string in one position only


      "Jon Kraft" <jon@jonux.co.u k> wrote in message
      news:Xns943D85B 786EB6jonjonuxc ouk@130.133.1.4 ...[color=blue]
      > "sinister" <sinister@nospa m.invalid> wrote:
      >[color=green]
      > > Is it possible to modify a string in one place using an indexing
      > > operator and an assignment statement?
      > >
      > > E.g.:
      > > $s = "1234567890 ";
      > > $s[5] = "d"; // $s is now "12345d7890 "[/color]
      >
      > Hi,
      >
      > Square brackets here are deprecated since 4.0, use curly braces:
      >
      > $s = "1234567890 ";
      > $s{5} = "d"; // $s is now "12345d7890 "[/color]

      My question wasn't really about {} or []; I already knew the difference.

      The line
      $s{5} = "d";
      (that is, altering a string by an indexing operator and an assignment
      statement) isn't documented anywhere either.


      [color=blue]
      >
      > HTH;
      > JOn[/color]


      Comment

      • Jon Kraft

        #4
        Re: modify string in one position only

        "sinister" <sinister@nospa m.invalid> wrote:
        [color=blue]
        > "Jon Kraft" <jon@jonux.co.u k> wrote:[color=green]
        >> "sinister" <sinister@nospa m.invalid> wrote:
        >>[color=darkred]
        >> > Is it possible to modify a string in one place using an indexing
        >> > operator and an assignment statement?
        >> >
        >> > E.g.:
        >> > $s = "1234567890 ";
        >> > $s[5] = "d"; // $s is now "12345d7890 "[/color]
        >>
        >> Square brackets here are deprecated since 4.0, use curly braces:
        >>
        >> $s = "1234567890 ";
        >> $s{5} = "d"; // $s is now "12345d7890 "[/color]
        >
        > My question wasn't really about {} or []; I already knew the difference.
        >
        > The line
        > $s{5} = "d";
        > (that is, altering a string by an indexing operator and an assignment
        > statement) isn't documented anywhere either.[/color]

        Your question was whether it was possible.

        From the manual:
        "Characters within strings may be accessed by specifying the zero-based
        offset of the desired character after the string in curly braces."



        Although the example given shows how to read certain characters in a
        string, the term "accessed" doesn't necessarily imply read-only.

        JOn

        Comment

        • sinister

          #5
          Re: modify string in one position only


          "Jon Kraft" <jon@jonux.co.u k> wrote in message
          news:Xns943DC81 074F67jonjonuxc ouk@130.133.1.4 ...[color=blue]
          > "sinister" <sinister@nospa m.invalid> wrote:
          >[color=green]
          > > "Jon Kraft" <jon@jonux.co.u k> wrote:[color=darkred]
          > >> "sinister" <sinister@nospa m.invalid> wrote:
          > >>
          > >> > Is it possible to modify a string in one place using an indexing
          > >> > operator and an assignment statement?
          > >> >
          > >> > E.g.:
          > >> > $s = "1234567890 ";
          > >> > $s[5] = "d"; // $s is now "12345d7890 "
          > >>
          > >> Square brackets here are deprecated since 4.0, use curly braces:
          > >>
          > >> $s = "1234567890 ";
          > >> $s{5} = "d"; // $s is now "12345d7890 "[/color]
          > >
          > > My question wasn't really about {} or []; I already knew the difference.
          > >
          > > The line
          > > $s{5} = "d";
          > > (that is, altering a string by an indexing operator and an assignment
          > > statement) isn't documented anywhere either.[/color]
          >
          > Your question was whether it was possible.
          >
          > From the manual:
          > "Characters within strings may be accessed by specifying the zero-based
          > offset of the desired character after the string in curly braces."
          >
          > http://uk.php.net/manual/en/language.types.string.php
          >
          > Although the example given shows how to read certain characters in a
          > string, the term "accessed" doesn't necessarily imply read-only.[/color]

          That's simply not clear. That they don't give an example of a write isn't
          encouraging. Nor do they illustrate this operation anywhere else in the
          manual, AFAICT.

          I do appreciate your having commented, however.
          [color=blue]
          >
          > JOn[/color]


          Comment

          Working...