Fortran分割字符串和字符串替换的子程序

Fortran分割字符串和字符串替换的子程序

!=============================================================
subroutine StringSplit(InStr,delimiter,StrArray,nsize)
!----------------------------------------------
!---将字符串InStr进行分割,结果放入StrArray中
!---delimiter::分隔符号,例如';,,' 使用;和,分割字符串
!---nsize:分割数目
!---吴徐平2011-04-29(wxp07@qq.com)
!----------------------------------------------
implicit none
character(len = *) , Intent( IN ) :: InStr
character(len = *)  , Intent( IN ) :: delimiter
character(len = LEN(InStr)),dimension(LEN(InStr)),Intent( OUT ) :: StrArray
integer, Intent( OUT ) :: nsize ! Effective Size of StrArray
integer:: i,j ! loop variable
integer:: istart ! split index for Start Position
nsize=0
istart=1
do i=1,LEN(InStr)
	do j=1,LEN(delimiter)
		if (InStr(i:i) == delimiter(j:j)) then
			if (istart == i) then
			istart=i+1 ! ---可防止分隔符相连的情况
			end if
			if (istart<i) then
				nsize=nsize+1
				StrArray(nsize)=InStr(istart:i-1)
				istart=i+1
			end if
		end if
	end do
end do
! ---匹配最后一个子字符串
if (nsize>0) then
	if (istart<LEN(InStr)) then
		nsize=nsize+1
		StrArray(nsize)=InStr(istart:LEN(InStr))
	end if
end if
! ---如果无可分割的子字符串,则包含整个字符串为数组的第一元素
if ( (nsize<1) .AND. (LEN(TRIM(InStr)) > 0 )) then
		nsize=1
		StrArray(1)=InStr
end if
end subroutine StringSplit
!
!=============================================================
subroutine StrReplace(InStr,OldChar,NewChar,OutStr)
!------------------------------------------------------------
!---将字符串InStr中的字符串OldChar替换成NewChar
!---结果放入字符串OutStr中
!---吴徐平2013-07-20(wxp07@qq.com)
!------------------------------------------------------------
implicit none
character(len = *) , Intent( IN ) :: InStr
character(len = *) , Intent( IN ) :: OldChar
character(len = LEN(OldChar)) , Intent( IN ) ::NewChar
character(len = LEN(InStr)) , Intent( INOUT ) :: OutStr
integer :: i  ! loop variable
OutStr=InStr
i=INDEX(OutStr,OldChar)
do while(i>0)
	OutStr(i:i+LEN(OldChar)-1)=NewChar
	i=INDEX(OutStr,OldChar)
end do
end subroutine StrReplace
!------------------------------------------------------------


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值