简体   繁体   English

如何使两个字符串作为参数的函数

[英]How to make a function with two strings as arguments

I'm not entirely sure what I'm doing wrong here. 我不确定自己在做什么错。 I have tested the code by input and output and it functions as desired no pun intended. 我已经通过输入和输出对代码进行了测试,它可以按预期的功能运行,而不是双关语。 :'P :'P

I'm just not setting up this function correctly and I believe it's because my arguments happen to be desirably a string. 我只是没有正确设置此函数,我相信这是因为我的参数恰好是字符串。 Where if done correctly "CD" will be inserted into the middle of "ABEF". 如果正确完成,将在“ ABEF”的中间插入“ CD”。 So, how do I go about doing this? 那么,我该怎么做呢?

Thanks! 谢谢!

insertstr(ABEF, CD)

Function insertstr(string1, string2)
nostrmsg = "No string"
fullng = len(string1)
half = len(string1)/2
if half>0 then hfstr1 = mid(string1, 1, half)
str2lng = len(string2)
if str2lng>0 then paste = hfstr1+string2
lshalf = mid(string1, half+1, fullng)
if str2lng+half=str2lng+half then insert = paste+lshalf
End Function

Start with the knowledge that a functions returns a value, a tentative specification of what the function should do, and a basic testing skeleton: 从了解函数返回值,对函数应执行的功能的初步说明以及基本测试框架开始:

Option Explicit

' returns the string build by inserting m(iddle) into f(ull) at half position
Function insertInto(f, m)
  insertInto = "?"
End Function

Dim t, r
For Each t In Array( _
   Array("ABEF", "CD", "ABCDEF") _
)
   r = insertInto(t(0), t(1))
   WScript.Echo t(0), t(1), r, CStr(r = t(2))
Next

output: 输出:

cscript 26873276.vbs
ABEF CD ? False

Then learn about Left , Mid , Len , and \\ (integer division) . 然后了解LeftMidLen\\(整数除法)

At last, re-write insertInto() so that the result starts with 最后,重新编写insertInto(),使结果以

cscript 26873276.vbs
ABEF CD ABCDEF True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM