开发者

Stripping data out of a string in Vbscript

I currently have a variable called databaseinformation in a script that I am writing. I want to seperate this into two variables called Instance_Name and Database_Name

The string in question:

[MSSQLSERVER]BESMgmt.BAK

In this instance the Instance_Name is MSSQLSERVER and the Database_Name is BESMgmt. The string will not necessarily end 开发者_如何学编程in .BAK but stripping the last four characters off the variable would be fine. The Instance_Name and Database_Name will change values and length.

Thanks for any help in advance


@Trinitrotoluene: Working sample code --

Option Explicit

Dim ToBeSplit, Instance_Name, Database_Name
Dim SplitMe
Dim Position

ToBeSplit = "[MSSQLSERVER]BESMgmt.BAK"

SplitMe = Split(ToBeSplit, "]")

If IsArray(SplitMe) Then
    Instance_Name = SplitMe(0)
    Database_Name = SplitMe(1)
End If

Instance_Name = Replace(Instance_Name, "[", "")

If InStr(Database_Name, ".") > 0 Then 
    Database_Name = Left(Database_Name, Len(Database_Name) - 4)
End If

Response.Write "Instance_Name = " & Instance_Name & "<br>"
Response.Write "Database_Name = " & Database_Name


Or do it with a regular expression object, this makes you more flexible, allthough some will say that you have two problems now:

Option Explicit
Dim regEx, matches
Dim myString : myString = "[MSSQLSERVER]BESMgmt.BAK"

set regEx = new RegExp
regEx.pattern = "\[(.*)\](.*).{4}"

set matches = regEx.Execute(myString)
msgbox "Instance_name: " & matches(0).subMatches(0) & vbNewLine & "Database_name: " & matches(0).subMatches(1)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜