modify string in one position only

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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.


    sinister Guest

  2. Similar Questions and Discussions

    1. Strip out from a position till end of string
      I'm trying to link to google maps from my website, but some of the addresses in the database I have, include the apartment number (eg. #2b), so I...
    2. Position of String Occurence
      Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :" Is there a function to find the position of the "I"th occurence of a...
    3. awk : find position of nth occurrence in string
      In awk, how can I find the position of the nth occurrence of a character in a string. I want to find the position of the 10th tab character in a...
    4. how can I modify a string within a file?
      I have a config file with various parameters defined and I want to change the value for one of the parms. The problem is that I have about 100 of...
    5. Return a Character String-position through a function
      Thanks in advance for your help! (MS Access 2000 Professional) I have the following function: Public Function EnterMfgPartNum(Placeholder As...
  3. #2

    Default Re: modify string in one position only

    "sinister" <sinister@nospam.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"
    Hi,

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

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

    HTH;
    JOn
    Jon Kraft Guest

  4. #3

    Default Re: modify string in one position only


    "Jon Kraft" <jon@jonux.co.uk> wrote in message
    news:Xns943D85B786EB6jonjonuxcouk@130.133.1.4...
    > "sinister" <sinister@nospam.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"
    >
    > Hi,
    >
    > Square brackets here are deprecated since 4.0, use curly braces:
    >
    > $s = "1234567890";
    > $s{5} = "d"; // $s is now "12345d7890"
    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.


    >
    > HTH;
    > JOn

    sinister Guest

  5. #4

    Default Re: modify string in one position only

    "sinister" <sinister@nospam.invalid> wrote:
    > "Jon Kraft" <jon@jonux.co.uk> wrote:
    >> "sinister" <sinister@nospam.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"
    >
    > 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.
    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."

    [url]http://uk.php.net/manual/en/language.types.string.php[/url]

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

    JOn
    Jon Kraft Guest

  6. #5

    Default Re: modify string in one position only


    "Jon Kraft" <jon@jonux.co.uk> wrote in message
    news:Xns943DC81074F67jonjonuxcouk@130.133.1.4...
    > "sinister" <sinister@nospam.invalid> wrote:
    >
    > > "Jon Kraft" <jon@jonux.co.uk> wrote:
    > >> "sinister" <sinister@nospam.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"
    > >
    > > 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.
    >
    > 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."
    >
    > [url]http://uk.php.net/manual/en/language.types.string.php[/url]
    >
    > Although the example given shows how to read certain characters in a
    > string, the term "accessed" doesn't necessarily imply read-only.
    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.
    >
    > JOn

    sinister Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139