如何在Powershell中逐个字符串拆分

编程入门 行业动态 更新时间:2024-10-26 19:37:24
本文介绍了如何在Powershell中逐个字符串拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图用一个分隔符来吐出字符串,这是一个字符串:

I'm trying to spit the string with a delimiter, which is a string:

$string = "5637144576, messag<>est<<>>5637145326, 1<<>>5637145328, 0" $separator = "<<>>" $string.Split($separator)

作为分裂的结果,我得到:

As the result of splitting I get:

5637144576, messag est 5637145326, 1 5637145328, 0

代替

5637144576, messag<>est 5637145326, 1 5637145328, 0

当我尝试使用接受 string[] 的重载拆分时:

When I try to use overloaded split which accepts string[]:

$string = "5637144576, messag<>est<<>>5637145326, 1<<>>5637145328, 0" $separator = @("<<>>") $string.Split($separator)

但我得到下一个错误:

Cannot convert argument "0", with value: "System.Object[]", for "Split" to type "System.Char[]": "Cannot convert value "<<>>" to type "System.Char". Error: "String must be exactly one character long.""

有人知道如何按字符串拆分字符串吗?

Does someone knows how to split string by string?

推荐答案

-split 运算符使用字符串进行拆分,而不是像 Split() 这样的字符数组:

The -split operator uses the string to split, instead of a chararray like Split():

$string = "5637144576, messag<>est<<>>5637145326, 1<<>>5637145328, 0" $separator = "<<>>" $string -split $separator 5637144576, messag<>est 5637145326, 1 5637145328, 0

如果你想对字符串使用Split()方法,你需要$seperator是一个只有一个元素的stringarray,并且还要指定一个stringsplitoptions值.您可以通过检查其定义来看到这一点:

If you want to use the Split() method with a string, you need the $seperator to be a stringarray with one element, and also specify a stringsplitoptions value. You can see this by checking its definition:

$string.Split OverloadDefinitions ------------------- string[] Split(Params char[] separator) string[] Split(char[] separator, int count) string[] Split(char[] separator, System.StringSplitOptions options) string[] Split(char[] separator, int count, System.StringSplitOptions options) #This one string[] Split(string[] separator, System.StringSplitOptions options) string[] Split(string[] separator, int count, System.StringSplitOptions options) $string = "5637144576, messag<>est<<>>5637145326, 1<<>>5637145328, 0" $separator = [string[]]@("<<>>") $string.Split($separator, [System.StringSplitOptions]::RemoveEmptyEntries) 5637144576, messag<>est 5637145326, 1 5637145328, 0

正如@RomanKuzmin 指出的那样,-split 默认使用正则表达式模式进行拆分.所以请注意转义特殊字符(例如 . 在正则表达式中是任何字符").您还可以强制 simplematch 禁用正则表达式匹配,例如:

As @RomanKuzmin pointed out, -split splits using regex-patterns by default. So be aware to escape special characters (ex. . which in regex is "any character"). You could also force simplematch to disable regex-matching like:

$separator = "<<>>" $string -split $separator, 0, "simplematch"

阅读有关 -split 的更多信息此处.

Read more about -split here.

更多推荐

如何在Powershell中逐个字符串拆分

本文发布于:2023-11-23 03:50:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619985.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何在   Powershell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!