Madura Harshana
Madura Harshana

Reputation: 1299

what is the javascript equivalent function for vbscript Mid function? is that substring() or substr()?

There are two javascript solutions for this,

1. String.substring(from,to) 
2. String.substr(from,length)

what is the most convenient solution for vb script Mid() function

Upvotes: 3

Views: 8572

Answers (5)

katspaugh
katspaugh

Reputation: 17899

substr is deprecated, you should use slice instead.

var str = 'hello';
str.slice(2, 4); // ll

Upvotes: 1

user1915322
user1915322

Reputation:

Syntax of mid function is follow,

 Mid(string,start[,length]) 

Mid function use length as a parameter.So, you have to use length.

 string.substr(start,length)

Upvotes: 1

JamesOR
JamesOR

Reputation: 1168

foo = "Hello World!".substr(2, 4);

is the same as

foo = Mid("Hello World!", 2, 4)

Upvotes: 2

Aesthete
Aesthete

Reputation: 18850

The method substr matches the usage of VB Mid function closest. The obvious difference is the the VB function takes a string as an argument, where the JS substr is a method of the string object.

string.substr(start,length)

Upvotes: 4

icktoofay
icktoofay

Reputation: 128991

It depends what you want to do. If in VBScript you were subtracting to find the length, substring may be the most convenient for you since you don't have to calculate the length. If, on the other hand, you have the length and would otherwise need to calculate the end position, substr will be more convenient for you.

Upvotes: 1

Related Questions