Jump to content

must be easier way for getting the count and locations of occurence in strings ?


Recommended Posts

must be an easier way for getting the count and locations of occurrences in strings

 

I know my code works but I just wanted to remove the last Directory folder from a string such as

C:\user\xxx\bbb\aaa 
to

C:\user\xxx\bbb\ 

 

#include <array.au3>
$s_Dir = @WorkingDir

$a_dir  = StringSplit($s_Dir,"\")

$1=$a_dir[0]
Global $A_doubleArray[$a_dir[0]][3]


For $i = 1 to $a_dir[0]
ConsoleWrite("Counting1: " & $i & @LF)
$A_doubleArray [($i - 1)][0]  = StringLen($a_dir[$i])

    $A_doubleArray [($i - 1)][1]  = $a_dir[$i]

    $A_doubleArray[($i - 1)][2] = 0

    If $i = 1 Then
    $A_doubleArray[($i - 1)][2] =  $A_doubleArray [($i - 1)][0]

    Else
$A_doubleArray [($i - 1)][2] = $A_doubleArray [($i - 1)][0] + $A_doubleArray [($i - 2)][2] + 1 ; (because of the removed '\'
$trimnumber = $A_doubleArray [($i - 2)][2]
    EndIf
    _ArrayDisplay($A_doubleArray)


If $i =$a_dir[0] Then $trimnumber = $A_doubleArray [($i - 1)][2] - $A_doubleArray [($i - 2)][2]
ConsoleWrite("Counting2: " & $i & @LF)


Next
MsgBox(0,"$s_Dir",$s_Dir)
$b_Dir = StringTrimRight($s_Dir,$trimnumber)
MsgBox(0,$trimnumber,$b_Dir)


_ArrayDisplay($A_doubleArray)

 

Thank you for the insight!

 

Link to comment
Share on other sites

@butterfly
Maybe something like this? :)

Global $strString = "C:\user\xxx\bbb\aaa", _ ; This MUST end with no /
       $arrResult, _
       $strNewString = ""
       

ConsoleWrite("Before: $strString = " & $strString & @CRLF)

$arrResult = StringSplit($strString, "\")
For $i = 1 To $arrResult[0] - 1 Step 1
    $strNewString &= $arrResult[$i] & "\"
Next

ConsoleWrite("After: $strNewString = " & $strNewString & @CRLF)

 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

This works as long as there is no trailing backslash:

#include <File.au3>
Global $sFilePath = "C:\user\xxx\bbb\aaa", $sDrive, $sDir, $sFilename, $sExtension
_PathSplit($sFilePath, $sDrive, $sDir, $sFilename, $sExtension)
MsgBox(0, "Result", $sDrive & $sDir)

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Another option:

MsgBox(0,"File Path", StringLeft("C:\user\xxx\bbb\aaa", StringInStr("C:\user\xxx\bbb\aaa", "\", 0, -1) - 1))

Or in a function

MsgBox(4096, "File Path", _Get_ParentDir("C:\user\xxx\bbb\aaa\"))

;~ $i_End = 0 - Exclude End Backslash
;~ $i_End = 1 - Include End Backslash
Func _Get_ParentDir($s_Dir, $i_End = 0)
    If StringRight($s_Dir, 1) = "\" Then $s_Dir = StringTrimRight($s_Dir, 1)
    Return StringLeft($s_Dir, StringInStr($s_Dir, "\", 0, -1) - $i_End)
EndFunc

 

Link to comment
Share on other sites

;~ $str = "C:\user\xxx\bbb\aaa"
$str = "C:\user\xxx\bbb\aaa\"

msgbox(0, '' , stringright($str , 1) = "\" ? stringtrimright($str , stringlen($str) - stringinstr($str , "\" , 0 , -2)) : stringtrimright($str , stringlen($str) - stringinstr($str , "\" , 0 , -1)))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

A regex way... 

$s1 = "C:\user\xxx\bbb\aaa"
$s2 = "C:\user\xxx\bbb\aaa\"

$pattern = '[^\\]+\\?$'
Msgbox(0,"", StringRegExpReplace($s1, $pattern, ""))
Msgbox(0,"", StringRegExpReplace($s2, $pattern, ""))

means : replace "one or more non-backslash and a backslash if exists, at the end of the string"  with nothing  :)

Edited by mikell
comment added
Link to comment
Share on other sites

@pixelsearch

I'd create a dedicated thread where everyone can talk about RegEx...

You know, there are a lot of RegEx experts/fans around here! :)

And, thanks @mikell for your explanations... They are really well appreciated ^_^

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...