回 帖 发 新 帖 刷新版面

主题:[讨论]字符串编辑

键入一句英文句子,再键入一个要被替换的字符和一个要替换成的字符。
例:
输入:This is a book.
      o e
输出:This is a beek.

回复列表 (共10个回复)

沙发

以下代码没测试,给出思路供参考(有可能有语法错误,毕竟好久没动QB了)

     input str             '原字符
     input a,b             'a为查找的字符,b为替换的字符
     for i=1 to len(str)
        if mid(str,i,1)=a then                    '在愿字符串中查找a
             str=left(str,i-1) & b & right(str,i+1) '找到后替换
        end if
     next i

板凳

上面的是VB里的代码,
在VB里没有必要自己写这些代码,用Replace函数就可以了.

在QB里,我们可以自己来写一个简单的Replace函数:

function myReplace$(a$,f$,r$)
  if instr(r$,f$)>0 then
   print "ERROR"
   exit function
  endif
  j%=len(f$)
  do 
   i%=instr(a$,f$)
   if i%>0 then a$=left$(a$,i%-1)+r$+mid$(a$,i%+j%)
  loop while i%
  myReplace$=a$
end function

3 楼

不愧moz,写的东西就是完善,呵呵。

4 楼

愧! 谁说不愧?

5 楼


 问 MOZ Replace函数在 QBASIC 里 是干什么用的?我书上介绍的不清楚。还请
你给我说说 。谢了。[em13][em18][em12]

6 楼

Replace( ) 是VB里的函数,用来替换字符串里的某个子串的。
QB里没有这个函数

7 楼

input Str$
input Cha$
for i=1 to length(Str%)
  if Str$(i)=Cha$ then Str$(i)=Cha$
next
print Str$
END

8 楼

QB有没有INSTR这个函数?

9 楼

把一楼的修改了一下:
input a$
input b$,c$
for i=1 to len(a$)
   if mid$(a$,i,1)=b$ then 
      a$=left$(a$,i-1)+c$+right$(a$,len(a$)-i)
   end if 
next i 
? a$

10 楼

第7楼的 Str$() 是关键词,不能用作变量

我来回复

您尚未登录,请登录后再回复。点此登录或注册