如何在fortran中用另一个字符替换字符串中的字符 [英] How do I replace a character in the string with another charater in fortran

查看:214
本文介绍了如何在fortran中用另一个字符替换字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串"f://asd/aww/asdf.txt",我需要将其更改为"f:\ asd \ aww \ asdf.txt".可行吗?

I have a string "f://asd/aww/asdf.txt" and I need to change it to "f:\asd\aww\asdf.txt". is it feasible?

推荐答案

我以前已经这样做过,所以这是一种递归的方法,用于替换输入 string 中与模式匹配的部分输入参数 search 给定的任意长度,输入参数 substitute 给定的替换字符串也具有任意大小.

I have done this in the past, so here is a recursive way of replacing parts of an input string that match the pattern of arbitrary length given by search input argument with a substitute string of also arbitrary size given by the input argument substitute.

module String_mod

    implicit none
    public

!***********************************************************************************************************************************
!***********************************************************************************************************************************

contains

!***********************************************************************************************************************************
!***********************************************************************************************************************************

    pure recursive function replaceStr(string,search,substitute) result(modifiedString)
        implicit none
        character(len=*), intent(in)  :: string, search, substitute
        character(len=:), allocatable :: modifiedString
        integer                       :: i, stringLen, searchLen
        stringLen = len(string)
        searchLen = len(search)
        if (stringLen==0 .or. searchLen==0) then
            modifiedString = ""
            return
        elseif (stringLen<searchLen) then
            modifiedString = string
            return
        end if
        i = 1
        do
            if (string(i:i+searchLen-1)==search) then
                modifiedString = string(1:i-1) // substitute // replaceStr(string(i+searchLen:stringLen),search,substitute)
                exit
            end if
            if (i+searchLen>stringLen) then
                modifiedString = string
                exit
            end if
            i = i + 1
            cycle
        end do
    end function replaceStr

!***********************************************************************************************************************************
!***********************************************************************************************************************************

end module String_mod

program testReplaceStr

use, intrinsic :: iso_fortran_env, only: output_unit
use String_mod
implicit none

write( output_unit, "(*(g0,:,' '))" ) replaceStr( string = "f://asd/aww/asdf.txt" &
                                                , search = "/" &
                                                , substitute = "\" &
                                                )

end program testReplaceStr

D:\>ifort /debug:full /Zi /CB /Od /Qinit:snan /warn:all /gen-interfaces /traceback /check:all /check:bounds /fpe-all:0 /Qdiag-error-limit:10 /Qtrapuv main.f90 -o main.exe
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 19.0.4.245 Build 20190417
Copyright (C) 1985-2019 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.22.27905.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:main.exe
-debug
-pdb:main.pdb
-subsystem:console
-incremental:no
main.obj

D:\>main.exe
f:\\asd\aww\asdf.txt

请记住,这种方法可能不是最佳的性能考虑,因为它涉及最终字符串构造的多个递归内存分配,具体取决于 search 模式在输入中出现的次数字符串.

Keep in mind that this approach is likely not the best for performance considerations as it involves multiple recursive memory allocations for the final string construction, depending on how many times the search pattern appears in the input string.

这篇关于如何在fortran中用另一个字符替换字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆