Register
It is currently Fri Jul 25, 2014 5:23 am

string modification


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sun Apr 24, 2011 1:52 am   

Joined: Sun Apr 24, 2011 1:43 am
Posts: 2
i need to replace a char at a particular index in the string...for instance in c++ it'd like:

Code:
std::cin>>index;
std::string str; str="hello";
str.at(index)='A';  // this line is what i want to replicate in BASH


how do i do this?


Top
 Profile  
 PostPosted: Sun Apr 24, 2011 3:56 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
hum, do I have to learn c++ to help you ?
Code:
str="hello"
at=2
modif=A
echo "$str -> ${str::$((at-1))}$modif${str:$at}"

hello -> hAllo


Top
 Profile  
 PostPosted: Sun Apr 24, 2011 5:39 am   

Joined: Sun Apr 24, 2011 1:43 am
Posts: 2
great concision !! thanks..
c++ code i thought'd explain my requirement better & faster than writing the same thing in theory


Top
 Profile  
 PostPosted: Sun Apr 24, 2011 7:29 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
input sample and expected output is the best.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP