Replacing String started with some characters android - android

I want to ask question.
I have a dynamic ocr result like "Prosdfad" or "Pro324sd". And so I want to replace the string to be "Protein". I searched on this site but I haven't found it.
Is there any turorial?

Something like:
if(str.startsWith("Pro")) {
str = "Protein";
}
Where str is your String object.
However, note that it's case-sensitive, so this won't match e.g. "prosdfad". And you might want to consider doing a startsWith check further ahead instead of reassigning the String, if the part to be replaced contains some useful information.
Here's the String documentation. It has lots of useful methods such as startsWith, toLowerCase, matches, to name a few.

Related

Changing text input to also treat "u" as "ü", "ss" as "ß", etc. for word suggestions

I have the following idea:
In German we have four extra letters (ä, ö, ü, ß) and I don't know any other language which has these vocals but I think French people with their accents also know this problem. We have a lot of apps in the Google Play store for cities, bus stations, trains and other stuff like that. Now it is really exhausting that we always have to write these letters if we are on the go. It would be much easier to write Munchen (=München [de] = Munich [en]), Osterreich (Österreich [de] = Austria [en]) or something like Uberwasserstrasse (Überwasserstraße [de] = Over-Water-Street [en]). So my question is now:
A lot of apps show suggestions for our just typed word. I think in the code it is something like this:
String current = editText.getText().toString();
db.lookUp(current); // Of course SQL statement
Can we hook this so that Android thinks that we have typed an ä, ö, ü, ß if we write an a, o, u, ss and the system looks for words with one of these vowels and suggests both? Here I do not want to ask for code - I want to discuss if we are able to write a hack or hook for the Android system. Also, root-rights can be assumed with the solution. I'm looking forward to your ideas.
You could do this the other way around, by "normalizing" typed characters into their related non-diacritical versions. You can use the java.Text.Normalizer class for this. A good snippet can be found in this article:
public static String removeAccents(String text) {
return text == null ? null :
Normalizer.normalize(text, Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
When applied to "Münich", this returns "Munich". That way, you can use a simple string comparison using these normalized versions.
This wouldn't work for "ß" though. If that's the only special case, you could handle it separately.
What you are looking for is called accent-insensitive collating sequence. SQLite's COLLATE operator can be used to do such searches, but I learned from another post that there might be bugs you'll need to look out for.

Taking first and last characters from string variable and inserting xxx in between them in android

I have a value in a string variable. I need to take first and last letters from the string variable. Append it with xxx in between them. For example, if the string variable value is "googleuser", then i should get the output as "gxxxr". How is this made? I tried many ways, suggested by google, but still didn't find anything which is helpful to me. Can some one suggest me a way for this. I tried the charat(index) function but it returned wrong results.
You could do that using the StringBuilder:
StringBuilder s = new StringBuilder();
s.append(text.charAt(0));
s.append("xxx");
s.append(text.charAt(text.length -1));
s.toString();
You could use regexp
String s = "aaa";
s.repalceAll("(.){1}(.)+(.){1}", "$1xxx$3");

How to search for exact word in a big Message?

How to search for one word in a big message in Android?
I have a text like "The sun always shines above the clouds". I wanna search for a single word, like "sun", and change it to an image. How to do this? Is there any way?
String word = "cat";
String text = "The cat is on the table";
Boolean found;
found = text.contains(word);
Regular Expressions in Java are the most flexible and powerful tools you can use to search and replace strings within other strings. Depending on where you display this data (eg. an HTML View perhaps?) you can replace the words with markup that can display an image or find the location in the string where you can break up elements to create TextViews vs ImageViews. On this latter case, another useful method within the String class might be the indexOf() or contains() methods.
To find the position of a given word in a string use the method
public int indexOf (String string)
For replacing strings with other strings you can use
public String replaceAll (String regularExpression, String replacement)
It is not clear what you mean with "I wanna search for single word like (sun) and change to an image"
An easy way is to use the String.replace method:
String source="The (sun) is shining.";
String replaced=source.replace('(sun)', '<img href="a_sun.png">');
See: http://javarevisited.blogspot.se/2011/12/java-string-replace-example-tutorial.html

Android: converting between Strings, SpannedStrings and Spannablestrings

I have a string resource called "foo". It may be a simple string... or it may contain HTML. This may change over time: I should be able to box it up as at least a SpannableString immediately upon reading whether it's HTML or not (but how??)
I want to get that raw CharSequence and first be able to display it as-is (the exact characters, not Android's "interpretation" of it). Right now I can't do that... toString() decides to rip out the parts it doesn't think I want to see.
I'd then like to be able to create a SpannableString from this and other Strings or SpannableStrings via concatenation using some method (none of the normal ones work). I'd like to then use that SpannableString to display the HTML-formatted text in a TextView.
This shouldn't be difficult, but clearly I'm not doing it right (there's very little info out there about this that I've found so far). Surely there is a way to accurately interconvert between between Strings, SpannedStrings and even Spannablestrings, without losing the markups along the way?
Note that I've already played with the somewhat broken Linkify, but I want better control over the process (no dangling unformatted "/"s, proper hrefs, etc.) I can get this all to work IF I stay in HTML at all steps, though I can't concatenate anything.
Edit 1: I've learned I can use the following to always ensure I get my raw string (instead of whatever Android decides it thinks the CharSequence really is). Nice... now, how to coax this into a SpannableString?
<string name="foo"><![CDATA[
<b>Some bold</b>
]]>
</string>
Edit 2: Not sure why this didn't work earlier, but... if foo1 and foo2 are strings marked up as above (as CDATA), then one can apparently do this:
String foo1 = (String)getResources().getText(R.string.foo1);
String foo2 = (String)getResources().getText(R.string.foo2);
SpannedString bar = new SpannedString(Html.fromHtml(foo1+foo2));
Curious: is there a more straightforward solution than this? Is this CDATA business actually necessary? It seems convoluted (but not as convoluted as never quite knowing what the resource type will be... String, Spannable, etc.)
I had the same problem. There are two solutions according to Google API Guides.
First is to escape < mark with < in the string resource. Unfortunately, String conversion removes the tag in the background.
Second is to use Format Strings instead of XML/HTML tags. It seems simpler, faster, and evades hidden conversion problems. getString(resource, ...) works like a printf(string, ...) here.
Both work and require some code to replace given part of the string anyway (handle tags or format strings). Enjoy! =)
It appears there isn't a more straightforward way to accomplish this.

in android application how to detect the occurence of substring in a text

I'm getting a text extracted from qrcode from my application, but as I am using zxing library its putting an extra DEMO at the end. How to remove it from there?
I was trying this but it does not work:
String aboutText = (Global.text).toString();
aboutText = aboutText.replace("DEMO", " ");
String.replace only takes chars as argument. Try String.replaceFirst(String, String) instead.
aboutText = aboutText.replaceFirst("DEMO", " ");
shout work. You can also take a look at replaceAll
Here you are: String. As for a newbie it will be quite helpful to learn more about strings in Java and the operations with them.

Categories

Resources