Rookie question: "Replace"-command? 
Author Message
 Rookie question: "Replace"-command?

Hi NG,

Is there a command in fortran to replace a certain character in a
string with another one?

For example, I have a string STRINGNAME containing a '-', which I would
like to replace with another character (e.g. 'O'). Is this possible?

Thanks in advance.

Regards,

M.L.



Wed, 04 Jun 2008 02:45:24 GMT  
 Rookie question: "Replace"-command?


Quote:
> Hi NG,

> Is there a command in Fortran to replace a certain character in a
> string with another one?

> For example, I have a string STRINGNAME containing a '-', which I would
> like to replace with another character (e.g. 'O'). Is this possible?

Certainly.  Use the Index function to find the character position of '-'.
Then replace the character at that position with 'O'.
For ex:
position = index (stringname, '-')
stringname(position:position) = 'O'
Jim
Quote:

> Thanks in advance.

> Regards,

> M.L.



Wed, 04 Jun 2008 02:50:13 GMT  
 Rookie question: "Replace"-command?
Hi Jim,

Thanks! I assume the stringname() command works directly on the
existing string (i.e. changes it..)?

/M.L.



Wed, 04 Jun 2008 02:59:41 GMT  
 Rookie question: "Replace"-command?

Quote:

> Hi NG,

> Is there a command in Fortran to replace a certain character in a
> string with another one?

> For example, I have a string STRINGNAME containing a '-', which I would
> like to replace with another character (e.g. 'O'). Is this possible?

Yep.

You can use the INDEX intrinsic to find the position of your selected character (or
substring) and then use regular substring referencing to replace it.

e.g. (Note: no syntax checking has been done on what follows, so consult your f95 text)

   i = INDEX(StringName,'-')
   StringName(i:i) = 'O'

You could also use the above to roll your own replace routine.

   CALL myReplace( StringName, '-', 'O' )

In f90/95:
   SUBROUTINE myReplace( String, OldChar, NewChar )
     CHARACTER(*), INTENT( IN OUT ) :: String
     CHARACTER(*), INTENT( IN )     :: OldChar, NewChar
     INTEGER :: i
     i = INDEX( String, OldChar )
     IF ( i /= 0 ) String(i:i) = NewChar
   END SUBROUTINE myReplace

You didn't say if you were doing f95 or f77 so (I think!) the following should work for
f77 (I'm mostly sure INDEX was part of f77.....) :
       SUBROUTINE myReplace( String, OldChar, NewChar )
         CHARACTER*(*) String, OldChar, NewChar
         INTEGER i
         i = INDEX( String, OldChar )
         IF ( i .NE. 0 ) String(i:i) = NewChar
       END

cheers,

paulv

--
Paul van Delst



Wed, 04 Jun 2008 03:10:53 GMT  
 Rookie question: "Replace"-command?

Quote:

> Thanks! I assume the stringname() command works directly on the
> existing string (i.e. changes it..)?

No. You misunderstand. There is no stringname "comand" (or statement, or
function). That was just a placeholder sample name for whatever the name
of your string is. Use the actual name of your string directly.

--
Richard Maine                     | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov            |       -- Mark Twain



Wed, 04 Jun 2008 03:16:20 GMT  
 Rookie question: "Replace"-command?
Ah, I get it. Thanks for the elaboration!

/M.L.

Quote:


> > Thanks! I assume the stringname() command works directly on the
> > existing string (i.e. changes it..)?

> No. You misunderstand. There is no stringname "comand" (or statement, or
> function). That was just a placeholder sample name for whatever the name
> of your string is. Use the actual name of your string directly.

> --
> Richard Maine                     | Good judgment comes from experience;
> email: my first.last at org.domain| experience comes from bad judgment.
> org: nasa, domain: gov            |       -- Mark Twain



Wed, 04 Jun 2008 04:57:49 GMT  
 Rookie question: "Replace"-command?
Hi Paul,

Thanks! I appreciate the help even though the question was answered
shortly before you posted.

regards,

M.L.

Quote:


> > Hi NG,

> > Is there a command in Fortran to replace a certain character in a
> > string with another one?

> > For example, I have a string STRINGNAME containing a '-', which I would
> > like to replace with another character (e.g. 'O'). Is this possible?

> Yep.

> You can use the INDEX intrinsic to find the position of your selected character (or
> substring) and then use regular substring referencing to replace it.

> e.g. (Note: no syntax checking has been done on what follows, so consult your f95 text)

>    i = INDEX(StringName,'-')
>    StringName(i:i) = 'O'

> You could also use the above to roll your own replace routine.

>    CALL myReplace( StringName, '-', 'O' )

> In f90/95:
>    SUBROUTINE myReplace( String, OldChar, NewChar )
>      CHARACTER(*), INTENT( IN OUT ) :: String
>      CHARACTER(*), INTENT( IN )     :: OldChar, NewChar
>      INTEGER :: i
>      i = INDEX( String, OldChar )
>      IF ( i /= 0 ) String(i:i) = NewChar
>    END SUBROUTINE myReplace

> You didn't say if you were doing f95 or f77 so (I think!) the following should work for
> f77 (I'm mostly sure INDEX was part of f77.....) :
>        SUBROUTINE myReplace( String, OldChar, NewChar )
>          CHARACTER*(*) String, OldChar, NewChar
>          INTEGER i
>          i = INDEX( String, OldChar )
>          IF ( i .NE. 0 ) String(i:i) = NewChar
>        END

> cheers,

> paulv

> --
> Paul van Delst




Wed, 04 Jun 2008 04:59:27 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. beginners "let"/"random" question

2. "command=" question

3. simple "clock" command question

4. newbie question "invalid command name"

5. a question about the "format" command

6. Questions/problems with Tcl "exec" command

7. replace string AFTER "size","initial", "next"

8. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

9. awk "search and replace"

10. What replaces "EXIT"?

11. Replacing "Shutdown" Action Button in LaunchPad

12. Replacing "\"s using re.sub

 

 
Powered by phpBB® Forum Software