仅在字符第一次出现时拆分 [英] Only split on the first occurence of a character

查看:74
本文介绍了仅在字符第一次出现时拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个类似的字符串

If I have a string like

foo:bar baz:count

并且我想在第一次出现 : 时进行拆分并返回一个只包含两个元素的数组:

and I want to split on the first occurrence of : and get an array returned which contains only two elements:

  • 一个字符串,它是第一个冒号之前的元素.
  • 一个字符串,它是第一个冒号之后的所有内容.

如何在 Powershell 中实现此目的?

How can I achieve this in Powershell?

推荐答案

-split 运算符允许您指定要返回的最大子字符串数:

-split operator allows you to specify maximum number of substrings to return:

'foo:bar baz:count' -split ':',2

这篇关于仅在字符第一次出现时拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆