LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-17-2003, 04:02 PM   #1
richhill
LQ Newbie
 
Registered: Sep 2003
Location: Mesa, AZ, USA
Distribution: SuSe 8.1, Mandrake 6.2
Posts: 12

Rep: Reputation: 0
Question Perl : Changing a single char in a string


I want to change a single character in a string


Example:

$mystring = "..........";

$n = argv[1];

#How do I change $mystring so that the nth character
# is now ^ ?

print $mystring; # ...^.......


I'm not inserting a char - I'm replacing.

Thanks
 
Old 09-17-2003, 04:22 PM   #2
richhill
LQ Newbie
 
Registered: Sep 2003
Location: Mesa, AZ, USA
Distribution: SuSe 8.1, Mandrake 6.2
Posts: 12

Original Poster
Rep: Reputation: 0
Well I guess I may have found the answer myself.

substr($mystring, $n, 1) = "^"

will do the trick.

Correct me if I'm wrong.
 
Old 09-17-2003, 04:31 PM   #3
allo
Member
 
Registered: Jan 2003
Location: Toulouse, France
Distribution: slackware 8.1+
Posts: 35

Rep: Reputation: 15
Hello,


A heavy way with regular expression :

$n =~ s/^(.{5})./$1\^/;

Here I suppose for example you want to change the 6th character in string - The reason for the 5 : 6 -1 between {}. The first "^" means the the beginning of the string and the second with the backslash before means the character "^" and not the beginning of the string like the first one.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
How to convert string to char? twirl Programming 27 10-13-2005 07:11 AM
c++ : replacing a char in a string cb951303 Programming 17 06-06-2005 05:48 PM
prepend char to a string schneidz Programming 6 06-01-2005 08:53 AM
Convert C++ string to C char* nyk Programming 3 06-17-2004 08:15 AM


All times are GMT -5. The time now is 02:46 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration