How to delete single character in a string scalar

L

Long Li

Hi,
I want to delete a character in a string scalar, for example, $str =
"abcabcabc", how can I delete the second "c" in $str, I mean after
deletion, $str becomes "abcababc".
Thanks.
 
J

Jürgen Exner

Long said:
I want to delete a character in a string scalar, [...]

perldoc -f substr
Or you can convert your string into an array of characters, then use
splice(), and then join() it back together.
Or use s to replace the third character with nothing (something along the
line of s/^(..)./$1/)
Or ....

jue
 
J

Jürgen Exner

Long said:
I want to delete a character in a string scalar, for example, $str =
"abcabcabc", how can I delete the second "c" in $str, I mean after
deletion, $str becomes "abcababc".

Coming to think of it: your question is not quite clear. You don't specify
the criteria by which you determine which character should be deleted.
- do you want to delete the n-th character in that string, e.g. in your
example the sixth character?
- do you want to delete the n-th character that has a specific value, e.g.
the second "c", no matter what its position within the string?
- do you want to delete the n-th character in that string, but only if it
has a specific value?
- ....?

Depening on your criteria the possible solution will be very different.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top