When a Salesforce Sandbox is created or refreshed, the username will be appended with the Sandbox Name. So, we can use UserInfo.getUserName() and find the sandbox name from the User’s Username value.
Sample Code:
UserInfo.getUserName().substringAfterLast('.').toUpperCase();