How to select a word from a String - android

I'm working on a smart EditText where the user write a product and a price without any worries about formatting (e.g. "Pasta 0,50" or "0.50 tomato juice").
I'd like to separate the price from all the rest to have something like
String input = "Cheese 2,00 for Pizza";
String outputProduct = "Cheese for pizza";
String outputPrice = "2,00";
Usually it shouldn't be a thing, but I'm also considering of taking care if the price isn't at the very start or the very end of the string, so if it's in the middle.
I also have to deal with periods and comms in prices.. since not everyone use the same kind of format.
What I've tryed so far: answer from OscarRyz here but it seems to be not very related with what I'm trying to achieve.
Also seen some array operations but my problem is to get the actual length of the price.
Any help would be very appreciated!

Related

Android Studio version of an Excel vlookup?

Ok I'm a massive noob and apart from following lots of tutorials I like to set myself a problem and then try to fix it with an app. Therefore I'm trying to make a little app that'll help me when I'm at work.
Basically it needs to breakdown a 4 character string into it's individual characters and then display them phonetically. So if I (the user) type in 5F9A then it'll display FIVE FOXTROT NINE ALPHA. At work we have an excel spreadsheet that does this all and I'm just trying to reverse engineer it. The spreadsheet itself has multiple stages, it reads the characters, converts them into ASCII and then performs a vlookup on a range of cells where each ASCII code is next to it's phonetic pronunciation. It looks for the number 53 (5 in ASCII) and then looks at the cell next to it which says FIVE.
I've managed to translate any user input into ASCII but I just don't know how to store and access this next set of data. I've been looking into SQLite but that is waaaaaay beyond me at the moment and seems far to complicated for something this simple?
Anyway, I know it's cheating asking for the answer, but maybe a push in the right direction?
The dummy way to do that would be:
Get every letter (char) of the word
Have a switch case that gives you the phonetic equivalent (you will have to do that by hand)
String word = yourWord;
String phonetic;
char currentChar;
for(i=0;i<=word.lenth();i++){
currentChar = word.substring(i, i+1);
phonetic = getPhonetic(currentChar)
}
String getPhonetic(char char){
switch char{
case a:
return alpha;
break;
case b:
....
}
}

Android find Text Group and Highlight in ListView

I am performing a search over Strings in an ArrayList. If the Term is found I need to highlight that term and return the a string with the word where the term is present plus a word before and after that word!
Example:
Term = “some”
Searched String = “This is my awesome test String!”
Result = “my awesome test” (“some” should be highlighted here)
First off I am useless with RegEx and wouldn’t know where to start and secondly I’m not sure how to highlight text in a ListView, there are 3 TextViews per Row and I pass an Array with Data objects to the Adapter. Can I just give the Data Object Spanndable’s for highlighting?!
After some trial and error and some help i got this which seems to do the trick
(^|\S*\s)\S*term\S*($|\s\S*)

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?
That is the way it is. I mean even I didn't like it, obviously user knows that he is buying from the app but I think Google is going to reply it in this way only
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.
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("");
String productTitleWithAppName=skuDetails.getTitle();
String productTitleWithoutAppName = productTitleWithAppName.substring(0, productTitleWithAppName.indexOf("("));
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 :)
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.
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.

Linkify Android Transform Question

Trying to do something fairly simple.
Taking text like this
User Name: This is a comment I am making
It is in a single TextView. I want to make the User Name a link. I decided that the easiest thing would be to surround the User Name with something like "$#" so it becomes
"$#User Name:$# This is a comment I am making
That way I can use the following regular expression
Pattern userName = Pattern.compile(".*\\$#(.+)\\$#.*");
with Linkify and make it a link. However, clearly I need to remove the delimiters, so the following is the code
title.setText(titleText);
Linkify.TransformFilter transformer = new Linkify.TransformFilter() {
#Override
public String transformUrl(Matcher match, String url) {
return match.group(1);
}
};
Linkify.addLinks(title, userName, "content://user=", null, transformer);
For some reason however, the whole text becomes one giant link, and the text isn't being transformed at all.
It actually did turned out to be pretty easy. I ended up not using the crazy "$#" to delimit the username, instead sticking with just
User Name: This is a comment I am making
so I ended up using the following pattern
Pattern userName = Pattern.compile("(.+:)");
Very simple, and the code becomes just
title.setText(titleText);
Linkify.addLinks(title, GlobalUtil.userName, "user://" + userId + "/");
Thank you to nil for the original suggestion. I was indeed matching the whole string instead of just the userName which is the link.
My best guess is the regex you're using is the problem, where you're telling it to basically pick out the entire string if it matches, including everything before and after what you're looking for. So, the TransformFilter is probably being passed the entire matched string. transformUrl as far as I can tell expects you to return the URL, so the entire string is linked to the first match group.
So, with that in mind, it's probably in your best interest to change the regex to something along the lines of "\\$#(.+?)\\$#" (with an added ? in the group to make the match non-greedy) so as to avoid matching the entire string and just picking out the bit you want to URL-ize (for lack of a better term, plus adding -ize to words sounds cool).
Why not put the delimiters inside the pattern to change ?
Pattern userName = Pattern.compile(".*(\\$#.+\\$#).*");
Then change the transform filter to remove the start and end patterns when changing into the URL...

Android TextViews. Is parametrization possible? Is binding to model possible?

I am new to both Android and Stack Overflow. I have started developing and Android App and I am wondering two things:
1) Is it possible to parametrize a TextView? Lets say I want to render a text message which states something like: "The user age is 38". Lets suppose that the user age is the result of an algorithm. Using some typical i18n framework I would write in my i18n file something like "The user age is {0}". Then at run time I would populate parameters accordingly. I haven't been able to figure out how to do this or similar approach in Android.
2) Let's suppose I have a complex object with many fields. Eg: PersonModel which has id, name, age, country, favorite video game, whatever. If I want to render all this information into a single layout in one of my activities the only way I have found is getting all needed TextViews by id and then populate them one by one through code.
I was wondering if there is some mapping / binding mechanism in which I can execute something like: render(myPerson, myView) and that automatically through reflection each of the model properties get mapped into each of the TextViews.
If someone has ever worked with SpringMVC, Im looking for something similar to their mechanism to map domain objects / models to views (e.g. spring:forms).
Thanks a lot in advanced for your help. Hope this is useful for somebody else =)
bye!
In answer to #1: You want String.format(). It'll let you do something like:
int age = 38;
String ageMessage = "The user age is %d";
myTextView.setText(String.format(ageMessage, age));
The two you'll use the most are %d for numbers and %s for strings. It uses printf format if you know it, if you don't there's a quicky tutorial in the Formatter docs.
For #2 I think you're doing it the best way there is (grab view hooks and fill them in manually). If you come across anything else I'd love to see it.

Categories

Resources