简体   繁体   中英

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

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". 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) .

At last, re-write insertInto() so that the result starts with

cscript 26873276.vbs
ABEF CD ABCDEF True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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