我目前在我编写的脚本中有一个名为databaseinformation的变量。我想将其分成两个名为Instance_Name和Database_Name
的变量有问题的字符串:
[MSSQLSERVER] BESMgmt.BAK
在此实例中,Instance_Name是MSSQLSERVER,Database_Name是BESMgmt。该字符串不一定以.BAK结尾,但从变量中删除最后四个字符就可以了。 Instance_Name和Database_Name将更改值和长度。
感谢您提前提供任何帮助
答案 0 :(得分:2)
@Trinitrotoluene:工作示例代码 -
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
答案 1 :(得分:1)
或者使用正则表达式对象,这会让你更灵活,虽然有人会说你现在有两个问题:
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)