파워셸의 문자열 배열
- PowerShell 문자열 배열
- 방법 1: 내장 방법을 사용하여 문자열 배열 선언
-
방법 2:
System.Collections.Arraylist
를 사용하여 문자열 배열 선언 - 문자열 배열의 길이 구하기
- 문자열 배열의 타입 구하기
- 문자열 배열에 문자열 추가
- 문자열 배열 안에서 문자열 찾기
- 문자열 배열의 문자열 대소문자 변경
- 문자열 배열의 모든 기존 메소드 가져오기

이 튜토리얼에서는 PowerShell에서 문자열 배열을 만드는 방법을 보여줍니다.
PowerShell 문자열 배열
PowerShell에서 문자열 배열을 선언하는 방법에는 여러 가지가 있습니다. 이 튜토리얼에서는 문자열 배열을 선언하는 다양한 방법과 문자열 배열에 접근하는 방법을 보여줍니다.
방법 1: 내장 방법을 사용하여 문자열 배열 선언
첫 번째 방법은 내장 PowerShell 방법을 사용하여 문자열 배열을 한 줄로 선언하는 것입니다. 명령어를 확인하세요:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
echo $DemoArray
위 코드에서는 문자열 배열을 생성한 후 이를 출력합니다. 출력 결과를 보세요:
This is Delftstack1
This is Delftstack2
This is Delftstack3
This is Delftstack4
방법 2: System.Collections.Arraylist
를 사용하여 문자열 배열 선언
System.Collections.Arraylist
는 배열과 배열 목록을 생성하는 데 사용되는 PowerShell 클래스입니다. 이 클래스를 사용하려면 먼저 이 클래스의 객체를 생성해야 합니다.
명령어를 확인하세요:
New-Object -TypeName System.Collections.Arraylist
$DemoArray = [System.Collections.Arraylist]@("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
echo $DemoArray
위 코드는 PowerShell의 System.Collections.Arraylist
클래스를 사용하여 문자열의 ArrayList
를 생성합니다. 출력 결과를 보세요:
This is Delftstack1
This is Delftstack2
This is Delftstack3
This is Delftstack4
문자열 배열의 길이 구하기
우리는 문자열 배열을 변수 $DemoArray
에 선언했기 때문에, 이 길이를 얻기 위해서는 변수와 함께 Length
메소드를 사용해야 합니다. 명령어를 보세요:
$DemoArray.Length
내장 방법에서 이 Length
메소드를 실행해야 합니다. 출력 결과를 확인하세요:
19
19
19
19
System.Collections.Arraylist
문자열 배열에서 이 Length
메소드를 사용하면 각 문자열의 길이를 보여줍니다. 예제를 확인하세요:
New-Object -TypeName System.Collections.Arraylist
$DemoArray = [System.Collections.Arraylist]@("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.Length
위 코드는 배열 내 각 문자열의 길이를 출력합니다. 출력 결과를 보세요:
19
19
19
19
문자열 배열의 타입 구하기
PowerShell의 GetType()
메소드를 사용하여 배열 타입을 얻을 수 있습니다. 위의 두 방법에서 문자열 배열의 타입을 얻어보겠습니다.
방법 1:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.GetType()
위 코드는 첫 번째 방법을 사용하여 문자열 배열의 타입을 얻을 것입니다. 출력 결과를 보세요:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
방법 2:
$DemoArray = [System.Collections.Arraylist]@("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.GetType()
위 코드는 System.Collections.Arraylist
방법을 사용하여 문자열 배열의 타입을 얻을 것입니다. 출력 결과를 보세요:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ArrayList System.Object
데이터 타입의 이름은 Name
열에 저장되므로 첫 번째 방법의 데이터 타입은 Object
, 두 번째 방법의 데이터 타입은 ArrayList
입니다. BaseType
열은 시스템 내 데이터 타입을 보여줍니다.
배열의 경우 항상 System.Array
이며, 문자열이나 ArrayList
의 경우 System.Object
가 됩니다.
문자열 배열에 문자열 추가
문자열 배열에 더 많은 문자열을 추가하거나 덧붙일 수는 +=
연결 연산자를 사용하여 수행할 수 있습니다. 예를 들어, 배열에 몇 개의 문자열을 추가하려면 아래 명령을 사용합니다:
$DemoArray += @("This is Delftstack5", "This is Delftstack6", "This is Delftstack7", "This is Delftstack8")
이제 문자열을 배열에 추가하고 길이를 확인한 후 배열을 출력하는 예제를 시도해 보겠습니다. 예제를 확인하세요:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
echo $DemoArray
$DemoArray += @("This is Delftstack5", "This is Delftstack6", "This is Delftstack7", "This is Delftstack8")
$DemoArray.Length
echo $DemoArray
위 코드는 문자열 배열을 생성하고 출력한 후 네 개의 문자열을 추가하고 배열의 길이를 확인한 다음 배열을 출력합니다. 출력 결과를 보세요:
This is Delftstack1
This is Delftstack2
This is Delftstack3
This is Delftstack4
8
This is Delftstack1
This is Delftstack2
This is Delftstack3
This is Delftstack4
This is Delftstack5
This is Delftstack6
This is Delftstack7
This is Delftstack8
문자열 배열 안에서 문자열 찾기
PowerShell에서 문자열 배열 안에서 문자열을 찾는 것은 쉽습니다. Contains()
메소드를 사용해야 하며, 매개변수는 찾고자 하는 문자열이 됩니다.
메소드는 문자열을 찾으면 true
를 반환하고, 그렇지 않으면 false
를 반환합니다. 우리 배열로 예제를 시도해 보겠습니다:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.Contains("This is Delftstack1")
$DemoArray.Contains("This is Delftstack5")
이 메소드는 contains()
메소드에서 주어진 문자열을 찾을 것이며, 문자열을 찾으면 true
를 반환하고 찾지 못하면 false
를 반환합니다. 출력 결과를 보세요:
True
False
문자열 배열의 문자열 대소문자 변경
우리는 문자열 배열 안의 문자열 대소문자를 변경할 수도 있습니다. 이를 위해 toLower()
및 toUpper()
메소드를 사용하여 각각 소문자와 대문자로 변환합니다.
예제를 시도해 보겠습니다:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray = $DemoArray.toLower()
echo $DemoArray
$DemoArray = $DemoArray.toUpper()
echo $DemoArray
위 명령들은 배열 문자열을 소문자와 대문자로 변환합니다. 출력 결과를 확인하세요:
this is delftstack1
this is delftstack2
this is delftstack3
this is delftstack4
THIS IS DELFTSTACK1
THIS IS DELFTSTACK2
THIS IS DELFTSTACK3
THIS IS DELFTSTACK4
문자열 배열의 모든 기존 메소드 가져오기
Get-Member
와 -MemberType
메소드를 사용하여 문자열 배열에 사용할 수 있는 모든 메소드를 가져올 수 있습니다. 문자열 배열에 사용할 수 있는 모든 메소드를 가져오려면 다음 명령을 실행하세요:
$DemoArray | Get-Member -MemberType Method
위 코드는 배열 $DemoArray
에 사용할 수 있는 모든 메소드를 가져옵니다. 출력 결과를 보세요:
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook관련 문장 - PowerShell Array
- PowerShell 다차원 배열
- PowerShell 배열에서 중복 값을 제거하는 방법
- PowerShell에서 객체 배열에 객체를 추가하는 방법
- PowerShell에서 배열 객체를 문자열로 변환하는 방법
- PowerShell 배열에 항목 추가하는 방법
- PowerShell에서 CSV 파일을 배열로 가져오는 방법