找回密码
 加入
搜索
查看: 34944|回复: 23

[系统综合] [已解决]StringSplit 如何得到最后一个拆分的字符串?

 火.. [复制链接]
发表于 2013-8-25 11:41:17 | 显示全部楼层 |阅读模式
本帖最后由 nikbobo 于 2013-8-25 16:40 编辑

例子:
$Var = "C:\Windows\Explorer.exe"
$a_Var = StringSplit($Var, "")
我的想法是:
$Var = "C:\Windows\Explorer.exe"
$a_Var = StringSplit($Var, "")
$Var2 = $a_Var[$a_Var[0]]
可是有几个问题:
1、$Var为空怎么办?
2、$Var不存在需要拆分的字符串怎么办?
3、假如$Var = "C:\Windows",这样拆分出来的是Windows,而不是我想要的xx.exe,怎么办?
4、也许$Var两侧包含英文双引号或英文单引号,怎么办?(如何先去除这些,必须先去除)

其实这个问题,简单来说,就是如何在给定@ScriptFullPath的情况下,将其变成@ScriptName,只不过AU3的宏仅针对脚本本身,而我是希望任何一个文件都可以。
发表于 2013-8-25 12:17:50 | 显示全部楼层
没太看懂楼主的意思啊 是不是有***.exe就返回 没有就不返回?
$var=StringRegExpReplace("C:\Windows\Explorer.exe",".+\\(.+\.exe)","$1")
If @extended Then MsgBox (0,"",$var)
发表于 2013-8-25 12:40:20 | 显示全部楼层
我猜测你是想获取路径最后的文件名和扩展名,正则实现的话
(?:^.*)\\(.*)$
非要用StringSplit的话
$Var = "C:\WINDOWS\system32\inetsrv\iis.msc"
$a_Var = StringSplit($Var, "",2)
MsgBox(0,0,$a_Var[UBound($a_Var)-1])
发表于 2013-8-25 12:44:18 | 显示全部楼层
正则肯定要方便些~
MsgBox(0, '', _FileGetNameFromPath('C:\Windows\Explorer.exe'))

Func _FileGetNameFromPath($sPath)
        Local $sName = StringRegExpReplace($sPath, '.*?([^\\]*?[^\\\s][^\\]*?$)', '$1')
        If Not @extended Then Return SetError(1, 0, '')
        Return $sName
EndFunc   ;==>_FileGetNameFromPath
发表于 2013-8-25 13:43:25 | 显示全部楼层
本帖最后由 netegg 于 2013-8-25 13:57 编辑

#Include <WinAPIEx.au3>
如果只要后缀的话
_WinAPI_PathFindExtension
字符串为空的话
_WinAPI_PathFindNextComponent
要文件名的话
_WinAPI_PathFindFileName
还有,那第三个问题是处理文件夹路径根本不包含文件名
要哪个用哪个,大不了加个条件判断
 楼主| 发表于 2013-8-25 13:56:13 | 显示全部楼层
本帖最后由 nikbobo 于 2013-8-25 14:01 编辑

回复 4# afan


1、$Var为空怎么办?
2、$Var不存在需要拆分的字符串怎么办?
3、假如$Var = "C:\Windows",这样拆分出来的是Windows,而不是我想要的xx.exe,怎么办?

这三个问题没解决。
如果出现这三个问题,我需要的是返回空。
 楼主| 发表于 2013-8-25 14:00:18 | 显示全部楼层
回复 5# netegg


同样也没解决:
#Include <WinAPIEx.au3>
$sPath = "C:\Windows"
$Test = _WinAPI_PathFindFileName ( $sPath )
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$Test' & @lf & @lf & 'Return:' & @lf & $Test & @lf & @lf & '@Error:' & @lf & @Error) ;### Debug MSGBOX
返回的是Windows。
发表于 2013-8-25 14:02:08 | 显示全部楼层
本帖最后由 netegg 于 2013-8-25 14:07 编辑

问一句,你那个path哪部分是文件名
#Include <WinAPIEx.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>

MsgBox(0,0,_WinAPI_PathFindFileName('C:\Windows\Explorer.exe'))
MsgBox(0,0,_WinAPI_PathFindExtension('C:\Windows\Explorer.exe'))

Global $Str, $Path = 'C:\Windows', $iMemo

_Main()

Func _Main()
    Local $hGUI, $i = 0

    ; 创建界面
    $hGUI = GUICreate("Path Parsing", 400, 300)
    GUISetState()

    ; 创建memo控件
    $iMemo = GUICtrlCreateEdit("", 2, 2, 396, 296, $WS_VSCROLL)
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")

    While $Path > ''
                $i += 1
      MemoWrite($i & " - "& $Path)
      $Path = _WinAPI_PathFindNextComponent($Path)
    WEnd

    ; 循环至用户退出
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>_Main

; 向memo控件写入信息
Func MemoWrite($sMessage = "")
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc ;==>MemoWrite
 楼主| 发表于 2013-8-25 14:04:17 | 显示全部楼层
本帖最后由 nikbobo 于 2013-8-25 14:05 编辑

回复 2# zch11230


    对了,但是判断方法能不能改一改。

也不对:
$var=StringRegExpReplace("Explorer.exe",".+\\(.+\.exe)","$1")
If @extended Then MsgBox (0,"",$var)
就不正常了。
 楼主| 发表于 2013-8-25 14:05:09 | 显示全部楼层
回复 8# netegg


    没明白意思。。
发表于 2013-8-25 14:08:59 | 显示全部楼层
本帖最后由 netegg 于 2013-8-25 14:10 编辑

回复 10# nikbobo
windows系统里除了host文件都有后缀名部分
你运行下上面的代码再说
发表于 2013-8-25 14:16:59 | 显示全部楼层
你是不是说只有文件名?
 楼主| 发表于 2013-8-25 14:19:44 | 显示全部楼层
回复 11# netegg

没明白什么意思。。。
发表于 2013-8-25 14:20:05 | 显示全部楼层
"C:\Windows"这个字符串哪包含xx.exe了
 楼主| 发表于 2013-8-25 14:23:21 | 显示全部楼层
回复 14# netegg


    但是你这个方法,看不明白,不会用。还不如二楼的方法,可是也有缺陷。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-6-2 23:35 , Processed in 0.086683 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表