Jump to content

String trim from specific part


Go to solution Solved by ad777,

Recommended Posts

Posted

Hi Team,

Can I trim URL from specific part? Like for example I just need maria as user in MsgBox output & not the whole URL.

I have used StringTrimLeft, but the issue is my URL keeps on changing from myurl.com to myurl1.com & so on. I am able to achieve using StringTrimLeft, but need more smart way to retrieve only the part after ?user=

Thanks!

 

#include <MsgBoxConstants.au3>
$link = "https://myurl.com/Dashboard.html?user=maria"
Local $sUser = StringTrimLeft($link, "39")
MsgBox(0, "", $sUser)

 

Posted (edited)

Hi @Iraj,

the solution that @ad777 already provided is fine ๐Ÿ‘ and the funny thing is that I used basically the same approach few years ago in an old tool ๐Ÿ˜€ .
Here my modified snippet:

Global $sUrl         = 'https://www.example.com/members.html?user=johndoe'
Global $sSearchAfter = '?user='

ConsoleWrite(_GetTextAfterSubString($sUrl, $sSearchAfter))

Func _GetTextAfterSubString($sText, $sSubString)
    Return StringMid($sText, StringInStr($sText, $sSubString) + StringLen($sSubString))
EndFunc

Best regards
Sven

________________
Stay innovative!

Edited by SOLVE-SMART

==> AutoIt related: ๐Ÿ”— GitHub, ๐Ÿ”— Discord Server, ๐Ÿ”— Cheat Sheet

  Reveal hidden contents
Posted
  On 2/23/2022 at 8:43 AM, Iraj said:

How can I get only the myurl.com part the similar way ?

Keeping in mind that myurl.com will change as myurl1 & so on...

Expand  
$link = "https://myurl.com/Dashboard.html?user=maria"
MsgBox(0,"",_GetStringBF($link,"https://","/Dash",5));;5 represent string len of /Dash
Func _GetStringBF($stringE,$before,$after,$ns)
    Local $SText
Local   $string = StringMid($stringE,StringInStr($stringE,$before)+StringLen($before),-1)
    For $i = 0  To StringLen($string)
    if StringMid($string,$i,$ns) = $after Then
        ExitLoop
    Else
        $SText &= StringMid($string,$i,1)
        EndIf
    Next
    Return $SText
    EndFunc

 

none

Posted (edited)

SRE could simplify your script :

#include <Constants.au3>
#include <Array.au3>

$link = "https://myurl.com/Dashboard.html?user=maria"
$arr = StringRegExp($link, "\/\/(.*)\/.*\?user=(.*)", 1)
_ArrayDisplay($arr)

 

Edited by Nine

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
ร—
ร—
  • Create New...