如何获取 PowerShell 字符串中最后一次出现字符的索引? [英] How to get the index of the last occurence of a char in PowerShell string?

查看:92
本文介绍了如何获取 PowerShell 字符串中最后一次出现字符的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 PowerShell 中的以下字符串中获取最后一个\"出现的索引,以便修剪活动"词并保留它:

I want to get the index of the last "\" occurrence in order to trim the "Activity" word and keep it, from following string in PowerShell:

$string = "C:\cmb_Trops\TAX\Auto\Activity"

我正在将代码从 VBScript 转换为 PowerShell,而在 VB 中有这个解决方案:

I'm converting the code from VBScript to PowerShell and in VB there's this solution :

Right(string, Len(string) - InStrRev(string, "\"))

使用 Right 和 InStrRev 函数让生活更轻松.不幸的是,我在 PowerShell 中没有找到类似的东西.找不到任何选项可以从字符串的末尾进行扫描.

Using Right and InStrRev functions which makes the life more easier. Unfortunately I didn't find anything like it in PowerShell. Can't find any option to scan from the end of the string.

推荐答案

$String.Split("\")[-1]

或者如果 $String 实际上是一个真实的路径,你可以考虑:

Or if $String is actually a real path, you might consider:

Split-Path $String -Leaf

这篇关于如何获取 PowerShell 字符串中最后一次出现字符的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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