'How can I avoid app names in inapp item titles returned from Google Play getSkuDetails?
I'd like the use the getSkuDetails() call from the In-app Billing v3 API to dynamically display a list of inapp purchase options with properly translated titles and relevant price.
However, the "title" property from getSkuDetails() seems to always be of the form "<item title> (app name)", which is less than useful. How can I get only the item title itself without the app name without hacking the string?
Solution 1:[1]
As no one has replied with an actual regex pattern to match the app name in parentheses in the SKU title I thought I just post the code here for further reference:
// matches the last text surrounded by parentheses at the end of the SKU title
val skuTitleAppNameRegex = """(?> \(.+?\))$""".toRegex()
val titleWithoutAppName = skuDetails.title.replace(skuTitleAppNameRegex, "")
The regex is as strict as possible to allow for additional text in parentheses within your SKU title without removing it as well (e.g. SKU titles like Premium (Subscription) would stay as they are). The only thing you should avoid is parentheses in your app name, but with a little tweaking of the regex you could work around that as well.
As regexes are notoriously expensive to build it is advisable to store it in a field and avoid constructing them each time over when you are parsing your SKUs.
Solution 2:[2]
Adapting @ubuntudroid answer to Java, I made it work like this:
String skuTitleAppNameRegex = "(?> \\(.+?\\))$";
Pattern p = Pattern.compile(skuTitleAppNameRegex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(skuDetails.getTitle());
String titleWithoutAppName = m.replaceAll("");
Solution 3:[3]
String productTitleWithAppName=skuDetails.getTitle();
String productTitleWithoutAppName = productTitleWithAppName.substring(0, productTitleWithAppName.indexOf("("));
Solution 4:[4]
You don't need to use regex for this. The following should work. This is in C# but you can guess how you can do it in Java or any other language.
string title = getTitle(); // get title
int appNameStartIndex = title.LastIndexOf("("); // find last index of (.
string titleWithoutAppName = title.Substring(0, appNameStartIndex); // and :)
Solution 5:[5]
In fact, the name of the SKU without the app name in parantheses is transfered with the SKU, but there is no getter to retrieve it.
If you have a deeper look into the implementation of SkuDetails class, then you see, that this whole thing is based on a json String (also debugging gives you this json).
And this json string contains not only the title with the apps name, but also a name field with the SKUs name only.
The SKUs json representation can be retrieved with SkuDetails.getOriginalJson().
So if it better fits your needs, then you can of course retrieve the SKU name directly from the data, returned from Google.
Solution 6:[6]
I found that in development builds, I'd get a value like My product (com.test.myapp (unreviewed)). Here's a version of the regex that handles the nested parentheses and keeps other parentheses in your product name intact:
const removeAppNameFromProductTitle = (title: string) => {
const regex = /( \([^()]*\)$)|( \([^)]*\)\)$)/im;
return title.replace(regex, '');
};
The code is in TypeScript (React Native), but I'm sure you can adapt. Here's a gist with test cases: https://gist.github.com/thmsobrmlr/732ecf958f600ec38e89c4e8ff57f3dd.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ubuntudroid |
| Solution 2 | enoler |
| Solution 3 | Mehdi Satei |
| Solution 4 | Bilal Khan |
| Solution 5 | CDoubleU |
| Solution 6 | Thomas Obermüller |
