Find character position and update file name

前端 未结 5 1256
离开以前
离开以前 2021-01-07 22:01

What function might I use to find a character position in a string using PowerShell 2.0.

i.e I would use CHARINDEX or PATINDEX if using SQL Server.

I looke

5条回答
  •  一整个雨季
    2021-01-07 22:29

    The string is a .NET string so you can use .NET methods. In your case:

    $index = "The string".IndexOf(" ")
    

    will return 3, which is the first occurrence of space in the string. For more information see: http://msdn.microsoft.com/en-us/library/system.string.aspx

    For your need try something like:

    $s.SubString($s.IndexOf("_") + 1, $s.LastIndexOf(".") - $s.IndexOf("_") - 1)
    

    Or you could use regexps:

    if ($s -Match '(_)(.*)(\.)[^.]*$') {  $matches[2] }
    

    (has to be adjusted depending on exactly what you need).

提交回复
热议问题