Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 你能把一个变量分成多个变量吗?_Powershell_Variables - Fatal编程技术网

Powershell 你能把一个变量分成多个变量吗?

Powershell 你能把一个变量分成多个变量吗?,powershell,variables,Powershell,Variables,有没有可能把一个变量分成两个独立的变量,我该怎么做呢。例如,以该字符串为例: $name = "firstname.surname" 把它吐进: $firstname $surname 使用split 使用.split方法。请注意,拆分的结果是一个数组,您必须选择数组中要分配给新变量的项(也称为索引): $firstname = $name.split(".")[0] $surname = $name.split(".")[1] 使用-split操作符。请注意,在下面的示例中,点“

有没有可能把一个变量分成两个独立的变量,我该怎么做呢。例如,以该字符串为例:

$name = "firstname.surname"
把它吐进:

$firstname 
$surname
使用
split
  • 使用
    .split
    方法。请注意,
    拆分的结果是一个数组,您必须选择数组中要分配给新变量的项(也称为索引):

    $firstname = $name.split(".")[0]
    $surname   = $name.split(".")[1]
    
  • 使用
    -split
    操作符。请注意,在下面的示例中,点“.”需要用前面的“\”转义,否则它将被解释为表示“任何字符”的正则表达式:

  • 或者,如果您确定将拆分为两个项目的数组:

    $firstname,$surname = $name -split("\.")
    
  • 阅读更多:

    使用正则表达式 您可以使用正则表达式拆分字符串“firstname.name”。此示例使用名为“text1”和“text2”的命名正则表达式组,这两个组一起匹配任何包含(至少)两位文本且中间有一个点的字符串

    $name = "firstname.surname"
    
    if($name -match '(?<text1>[^.]+)\.(?<text2>[^.]+)'){
      $firstname = $matches['text1']
      $surname   = $matches['text2']
    }
    
    $name=“firstname.name”
    如果($name-match'(?[^.]+)\.(?[^.]+)')){
    $firstname=$matches['text1']
    $姓氏=$匹配项['text2']
    }
    
    我确定您的示例中是想在“firstname.name”周围加引号?
    $name = "firstname.surname"
    
    if($name -match '(?<text1>[^.]+)\.(?<text2>[^.]+)'){
      $firstname = $matches['text1']
      $surname   = $matches['text2']
    }