.Net dev here, somewhat new to java. I have the following snippet of code. I’m trying to get the last segment of the the encoded url, but as you can see it’s cutting off the trailing “%25” and making that just “%”. That causes the URlDecoder.Decode to fail because the string is no longer properly encoded.
Ideally I’d like to use Path.getName() because from what I gather that won’t include query params. Also I’d really like to avoid 3rd party libraries if possible (this seems like a pretty basic task). Does anyone know what I’m doing wrong here, is there a way to get Path.getName() to not cut off the encoded “%25” at the end?
var playerSrcPath = URI.create("https://www.speedrun.com/api/v1/guests/Please+Submit+to+Any%25").getPath();
var path = Paths.get(playerSrcPath);
var lastSegment = path.getName(path.getNameCount() - 1).toString();
System.out.println(lastSegment);
// this errors, lastSegment got cut off, no longer properly encoded.
var playerAbbr = URLDecoder.decode(lastSegment, StandardCharsets.UTF_8);
System.out.println(playerAbbr);
4
In utf-8 %25 is a percent sign, I think that’s your issue. If you need the “25” the url would have to be like this …Please+Submit+to+Any%2525
If you’re using the % sign as sort of separator, you can then remove it in the resulting string with playerAbbr.replace(“%”, “”)
Let me know if that helps 🙂
4