programing

문자열을 별도의 변수로 분할

sourcejob 2023. 5. 14. 10:34
반응형

문자열을 별도의 변수로 분할

코드를 사용하여 분할한 문자열이 있습니다.$CreateDT.Split(" ")이제 다른 방법으로 두 개의 개별 문자열을 조작하려고 합니다.어떻게 이것들을 두 개의 변수로 나눌 수 있습니까?

이렇게요?

$string = 'FirstPart SecondPart'
$a,$b = $string.split(' ')
$a
$b

배열은 다음을 사용하여 생성됩니다.-split교환입니다.이렇게.

$myString="Four score and seven years ago"
$arr = $myString -split ' '
$arr # Print output
Four
score
and
seven
years
ago

특정 항목이 필요한 경우 배열 인덱스를 사용하여 항목에 도달합니다.인덱스는 0부터 시작합니다.이렇게.

$arr[2] # 3rd element
and
$arr[4] # 5th element
years

두 기법의 차이는 다음과 같습니다.

$Str="This is the<BR />source string<BR />ALL RIGHT"
$Str.Split("<BR />")
This
is
the
(multiple blank lines)
source
string
(multiple blank lines)
ALL
IGHT
$Str -Split("<BR />")
This is the
source string
ALL RIGHT 

이것으로부터 당신은 볼 수 있습니다.string.split() 방법:

  • 대소문자를 구분하는 분할을 수행합니다("R"에서 "ALL RIGHT"로 분할하지만 "Broken"은 "R"에서 분할되지 않습니다).
  • 문자열을 분할할 수 있는 문자 목록으로 처리합니다.

그 동안에-split 연산자:

  • 대소문자를 구분하지 않는 비교 수행
  • 전체 문자열에 대한 분할만

사용해 보십시오.

$Object = 'FirstPart SecondPart' | ConvertFrom-String -PropertyNames Val1, Val2
$Object.Val1
$Object.Val2

각 개체에 대한 작업 문:

$a,$b = 'hi.there' | foreach split .
$a,$b

hi
there

언급URL : https://stackoverflow.com/questions/30617758/splitting-a-string-into-separate-variables

반응형