473,503 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 17 '05 #1
4 4227
"sinister" <si******@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
Jul 17 '05 #2

"Jon Kraft" <jo*@jonux.co.uk> wrote in message
news:Xn**************************@130.133.1.4...
"sinister" <si******@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

Jul 17 '05 #3
"sinister" <si******@nospam.invalid> wrote:
"Jon Kraft" <jo*@jonux.co.uk> wrote:
"sinister" <si******@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."

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.

JOn
Jul 17 '05 #4

"Jon Kraft" <jo*@jonux.co.uk> wrote in message
news:Xn**************************@130.133.1.4...
"sinister" <si******@nospam.invalid> wrote:
"Jon Kraft" <jo*@jonux.co.uk> wrote:
"sinister" <si******@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."

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.


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

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8687
by: David Carter-Hitchin | last post by:
Hi, I'm not very experienced with c++ so I'm sure this is something obvious that is easily solved, but for the life of me I can't seem to figure it out. I'd be very grateful for some...
11
3174
by: Konrad Den Ende | last post by:
I have a function returning a string but the problem is that the color of it is blue which suits me well for some pages but not for others. Is it possible to "feel" what the color of the background...
32
14761
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
6
1707
by: bugnthecode | last post by:
I'm writing a program to send data over the serial port. I'm using pyserial, and I'm on WindowsXP. When I use literals I can get the data accross how I want it for example: 1 2 3 4...
31
3456
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I modify the current browser window?...
1
3039
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
23
1935
by: no1zson | last post by:
I have been adding buttons to my GUI to manipulate list data. I added a Delete button this morning in case I decide I no longer needed a particular element. I am now working on a modify button, in...
3
5077
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
2
13430
by: Hvid Hat | last post by:
class Sprite { private Vector2 position; public Vector2 Position { get { return position; } set { position = value; } } ... }
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.