Android Question Solved: replace string error

Ferdari

Active Member
Licensed User
Longtime User
I need to replace a portion of a string but my code does nothing.
Value contains an http://www.myserver.com/images/image-res.php?q=90&zc=2&w=100&src=picture.jpg that needs to be http://www.myserver.com/images/picture.jpg with replace function, but i always get the same, nothing is replaced.

My code

B4X:
Sub clv_ItemClick (Index As Int, Value As Object)
  Dim job As HttpJob
  job.Initialize("j", Me)
  Dim pdown1 As String
  pdown1=Value
  pdown1 = pdown1.Replace("/image-res.php?q=90&zc=2&w=100&src=", "/")
  job.Download(pdown1)
End Sub

Any idea? im newbie
 

Ferdari

Active Member
Licensed User
Longtime User
Are you sure that Value contains that text?
Add:
Log (Value)
yep it contains the URL and then is assigned to pdown1 then converted/replaced text, and downloaded without problem.
but with the image-res.php it only download a thumbnail.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
@Ferdari Please note that the order of the get-parameters in this string must be exact like this order..
q=90&zc=2&w=100&src=
ok
q=90&w=100&zc=2&src=
not ok

Otherwise you could do something like that.

Use the part of url beginning after the questionmark in a new string
-> q=90&zc=2&w=100&src=picture.jpg
Split the string at "&"
You get this resultS
q=90
zc=2
w=100
src=picture.jpg


Split each part on =
q=90
q
90

zc=2
zc
2

w=100

w
100

src=picture.jpg

src
picture.jpg

In this way the order of the get-parameters does not matter. you just need to check if the first result in splitting the parts is "src". The second result must be the filename

You can build a new url now...
 
Upvote 0

Ferdari

Active Member
Licensed User
Longtime User

Really appreciated, but that is not my problem, this php creates a thumbnail of a picture, with params: q=quality, zc=image crop, w=width, and src=source of file, but it works great. i just only want to delete the thumbnailer, by deleting the php from the string

the problem is here:
B4X:
Sub clv_ItemClick (Index As Int, Value As Object)      '<<--Value retrieved
  Dim job As HttpJob
  job.Initialize("j", Me)
  Dim pdown1 As String
  pdown1=Value         '<<--Value assigned to pdown1
  pdown1 = pdown1.Replace("/image-res.php?q=90&zc=2&w=100&src=", "/")     '<<--tried to replace the image-res php script without luck
  job.Download(pdown1)  '<<--Download starts fine
'if i check with LOG (pdown1) nothing is replaced
Log(pdown1)
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim url As String = "http://www.myserver.com/images/image-res.php?q=90&zc=2&w=100&src=picture.jpg"
    Dim getpars As String =  url.SubString(url.LastIndexOf("?")+1)
    Dim imagename As String = ""
    Log("getpars="&getpars)
    url = url.SubString2(0,url.LastIndexOf("/")+1)
    Log("url="&url)
  
    Dim gets() As String
    gets = Regex.Split("&",getpars)
    For i = 0 To gets.Length-1
        Dim params() As String
        params = Regex.Split("=",gets(i))
        Log(params.Length)
        If params.Length = 2 Then
            If params(0) = "src" Then
                imagename = params(1)
            End If
        End If
    Next
    Dim newurl As String = url&imagename
    Log(newurl)

Note with such a code the order of the get-parameters does NOT matter.

It´s not a solution for everything but for your issue it should work
 
Upvote 0

Ferdari

Active Member
Licensed User
Longtime User


How i do to replace /image-res.php?q=90&zc=2&w=100&src= with /
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
I think could be because the Replace function might be using a syntax similar to Regex, which means the question mark character could be a reserved special character. Not sure if this will work, try escaping ? with a \ like this:
B4X:
pdown1 = pdown1.Replace("/image-res.php\?q=90&zc=2&w=100&src=", "/")
 
Upvote 0

Ferdari

Active Member
Licensed User
Longtime User
Solved: it was problem with my server config giving me different params than mine, now i fixed it,

Thanks to everyone!
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim url As String = "http://www.myserver.com/images/image-res.php?q=90&zc=2&w=100&src=picture.jpg"
    Dim pos1 As Int = url.LastIndexOf("/")
    Dim pos2 As Int = url.LastIndexOf("=")
    Log(url.SubString2(0, pos1 + 1)&url.SubString(pos2 + 1))
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…