수색…


비고

MSDN 날짜 / 시간, 문자열 및 숫자 함수

https://msdn.microsoft.com/en-us/library/3ca8tfek(v=vs.84).aspx

1. 표준 문자열

vbscript에서 개체는 반드시 지정된 형식을 필요로하지 않습니다. C #의 var 변수와 비슷합니다.

Dim ExampleString1 As String
Dim ExampleString2

2. 문자열 조작 기본 사항

'Base string
Dim exStr : exStr = " <Head>data</Head> "

'Left
Dim res: res = Left(exStr,6) 'res now equals " <Head"
'Right
Dim res: res = Right(exStr,6) 'res now equals "Head> "
'Mid
Dim res: res = Mid(exStr,8,4) 'res now equals "data"
'Replace
Dim res: res = Replace("variable", "var", "") 'res now equals "riable"
'LCase
Dim res: res = Lcase(exStr) 'res now equals " <head>data</head> "
'UCase
Dim res: res = UCase(exStr) 'res now equals " <HEAD>DATA</HEAD> "
'LTrim
Dim res: res = LTrim(exStr) 'res now equals "<Head>data</Head> " notice no space on left side
'RTrim
Dim res: res = RTrim(exStr) 'res now equals "<Head>data</Head> " notice no space on right side
'Trim
Dim res: res = Trim(exStr) 'res now equals "<Head>data</Head>"
'StrReverse
Dim res: res = StrReverse(exStr) 'res now equals " >daeH/<atad>daeH< "
'String
Dim res: res = String(4,"c") 'res now equals "cccc"


'StrComp - String Compare, by default, compares the binary of 2 strings.
'The third parameter allows text comparison, but does not compare case(capitalization). 
'Binary
'-1 = if Binary structure of "cars" < "CARS"
' 0 = if Binary structure of "cars" = "cars"
' 1 = if Binary structure of "CARS" > "cars"
Dim res: res = StrComp("cars", "CARS") 'res now equals -1
Dim res: res = StrComp("cars", "cars") 'res now equals 0
Dim res: res = StrComp("CARS", "cars") 'res now equals 1

'Text
'-1 = if Text structure of "cars" < "CARSSS"
' 0 = if Text structure of "cars" = "cars"
' 1 = if Text structure of "CARSSS" > "cars"
Dim res: res = StrComp("cars", "CARSSS", 1) 'res now equals -1
Dim res: res = StrComp("cars", "cars", 1) 'res now equals 0
Dim res: res = StrComp("CARSSS", "cars", 1) 'res now equals 1

'Space
Dim res: res = "I" & Space(1) & "Enjoy" & Space(1) & "Waffles" 'res now equals "I Enjoy Waffles"

'Instr - Returns position of character or string in the variable.
Dim res: res = Instr(exStr, ">") ' res now equals 6
'InstrRev - Returns position of character or string in the variable from right to left. 
Dim res: res = Instr(exStr, ">") ' res now equals 2

'Split and Join
'These are methods that can be used with strings to convert a string to an array
'or combine an array into a string
Dim res1 : res1 = Split(exStr, ">")
'res1(0) = " <Head"
'res1(1) = "data</Head"
'res1(2) = " "
Dim res2 : res2 = Join(res1, ">")
'res2 now equals " <Head>data</Head> "    

3. 문자열 검색하기

주어진 텍스트 파일 test.txt :

Ford
Jeep
Honda

다음 스크립트는이 텍스트 파일을 처리하고 있습니다.

'Read in File Data to an array, separate by newline vb equivalent (vbcrlf)
Dim car, cars
Dim filefullname : filefullname = "C:\testenv\test.txt"
cars = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(filefullname, 1).ReadAll, vbcrlf)

'Exact Match search. 
Dim searchstring : searchstring = "Jeep"
For Each car In cars
    If Car = searchstring Then Exit For
Next

'Partial Match search
'Instr returns >0 if any result is found, if none, InStr returns -1
'The If statement will use -1 = false, >0 = true
Dim searchstring : searchstring = "Jee"
Dim position
For car = 0 To ubound(cars)
    If InStr(cars(car), searchstring) Then 
        position = car
        Exit For
    End If
Next

5. 시작 및 끝 문자를 통해 문자열의 특정 텍스트를 배열에 채 웁니다.

'Note: I use this method to extract non-html data in extracted GET telnet results
'This example effectively grabs every other vehicle that have start and finish
'characters, which in this case is "/". 
'Resulting in an array like this:
'extractedData(0) = "/Jeep"
'extractedData(1) = "/Ford"
'extractedData(2) = "/Honda"


Dim combined : combined = Join(cars, "/") & "/" & Join(cars, "/")
'combined now equals Ford/Jeep/Honda/Ford/Jeep/Honda

Dim record, trigger : record = false : trigger = false
Dim extractedData() : ReDim extractedData(0)
For I = 1 to len(combined) 'searching the string one character at a time
    If trigger Then 'if I've already started recording values
        If Mid(combined, I, 1) = "/" Then 'End Character is found, stop recording
            record = false
            trigger = false
            ReDim Preserve extractedData(ubound(extractedData)+1) 'Prep next Array Entry
        End If
    Else
        If Mid(combined, I, 1) = "/" Then record = true 'Start recording on start character 
    End If
    If record Then 
        'Increment text on array entry until end variable is found. 
        extractedData(ubound(extractedData)) = extractedData(ubound(extractedData)) & Mid(combined, I, 1) 
        trigger = true
    End If
Next

4. 문자열 조작 방법을 함께 연결합니다.

Dim exStr : exStr = " <Head>data</Head> "

Dim res
res = Ucase(Replace(Mid(exStr, instr(exStr, ">")+1,4), "ata", "ark")) 
'res now equals DARK
'instr(exStr, ">") returns 7
'Mid(" <Head>data</Head> ", 7+1, 4) returns "data"
'Replace("data",  "ata", "ark") returns "dark"
'Ucase("dark") returns "DARK"


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow