开发者

Doing assignment in VBscript now.. Need to give positions of each "e" in a string

I've done this in JavaScript but needless to say I can't just swap it over.

In Jscript I used this:

 var estr = tx_val
 index = 0
 positions = []

 while((index = estr.indexOf("e", index + 1)) != -1)
 {
     positions.push(index);
 }

document.getElementById('ans6').innerHTML = "Locations of 'e' in string the are: " 
+ positions;

I tried using the same logic with VBS terms, ie join, I also tried using InStr. I'm just not sure how to yank out that 'e'... Maybe I'll try replacing it with another character.

Here is what I tried with VBScript. I tried using InStr and replace to yank out the first occurance of 'e' in each loop and replace it with an 'x'. I thought that maybe this would make the next loop through give the location of the next 'e'. -- When I don't get a subscript out of range 'i' error, I only get one location back from the script and its 0.

(6) show the location of each occurence of the character "e" in the string "tx_val" in the span block with id="ans6"

countArr = array()
countArr = split(tx_val)
estr = tx_val
outhtml = ""
positions = array()
i=0
for each word in countArr
i= i+1
positions(i) = InStr(1,estr,"e",1)
estr = replace(estr,"e","x",1,1)

next

document.getElementById("ans6").innerHTML = "E is located at: " & positions

What can I do that is simpler than this and works? and thank you in advance, you all help a l开发者_StackOverflowot.

EDIT AGAIN: I finally got it working right. I'm not 100% how. But I ran through the logic in my head a few dozen times before I wrote it and after a few kinks it works.

 local = ""
 simon = tx_val
 place=(InStr(1,simon,"e"))
 i=(len(simon))
 count = tx_val

 do
local = (local & " " & (InStr((place),simon,"e")))
place = InStr((place+1),simon,"e")
count = (InStr(1,simon,"e"))
 loop while place <> 0

 document.getElementById("ans6").innerHTML= local


InStr has slightly different parameters to indexOf:

InStr([start, ]string, searchValue[, compare])
  • start: The index at which to start searching
  • string: The string to search
  • searchValue: The string to search for

Also note that Visual Basic indexes strings beginning at 1 so all the input and return index values are 1 more than the original JavaScript.


You can try split(). For example a simple string like this:

string = "thisismystring"

Split on "s", so we have

mystring = Split(string,"s")

So in the array mystring, we have

thi   i   my  tring
 ^    ^    ^    ^
[0]  [1]  [2]  [3]

All you have to do is check the length of each array item using Len(). For example, item 0 has length of 3 (thi), so the "s" is at position 4 (which is index 3). Take note of this length, and do for the next item. Item 1 has length of 1, so we add it to 4, to get 5, and so on.

@Update, here's an example using vbscript

thestring = "thisismystring"
delimiter="str"
mystring = Split(thestring,delimiter)
c=0
For i=0 To UBound(mystring)-1
    c = c + Len(mystring(i)) + Len(delimiter)
    WScript.Echo "index of s: " & c - Len(delimiter) 
Next

Trial:

C:\test> cscript //nologo test.vbs
index of str is: 8
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜