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

Spoiler

๐ŸŒย Au3Forums

๐ŸŽฒ AutoIt (en) Cheat Sheet

๐Ÿ“Š AutoIt limits/defaults

๐Ÿ’Ž Code Katas: [...] (comming soon)

๐ŸŽญ Collection of GitHub users with AutoIt projects

๐Ÿžย False-Positives

๐Ÿ”ฎย Me on GitHub

๐Ÿ’ฌย Opinion about new forum sub category

๐Ÿ“‘ย UDF wiki list

โœ‚ย VSCode-AutoItSnippets

๐Ÿ“‘ย WebDriver FAQs

๐Ÿ‘จโ€๐Ÿซย WebDriver Tutorial (coming soon)

Posted (edited)
2 hours ago, ad777 said:
#include <MsgBoxConstants.au3>
$link = "https://myurl.com/Dashboard.html?user=maria"
Local $sUser = StringMid($link,StringInStr($link,"?user")+6,-1)
MsgBox(0, "", $sUser)

ย 

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

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

Edited by Iraj
Posted
2 hours ago, 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...

$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...