How find text programmatically? - android

Is there any way to find the text programmatically?
I would like put a extensive text in a TextView or other way, and then to search for specific words, as if you used Ctrl + F. I don't found information, I found SearchView but these search data only of a View, no?
Thanks in advance.

You can use the method .contains provided by the String class that "Returns true if and only if this string contains the specified sequence of char values" (Oracle Docs) within your algorithm for searching for text then based on what that method returns execute certain code to do what you want if the CharSequence exists or not.
String Class (Oracle)

Related

Skip suggestion string value from Android EditText

I have an Android EditText with suggestions enabled. My goal is not to disable suggestion (since i'm able to do that), but to skip some string values from them.
For example, suppose that i'd like to skip 'hello' word.
If i write 'he' in the EditText, my suggestions list shouldn't contain 'hello' since it's a string that has to be skipped. Is it possible?
Hope i explained myself, thanks

Preserve text case when replacing based on case-insensitive search?

In my app I have a Textview with some text. I'm trying to get an input from the user, and then highlight words in the Textview according to that input.
For instance if the text is
Hello stackoverflow
and the input for the user is
hello
I want to replace the text with:
<font color='red'>Hello</font>` stackoverflow
This is my code:
String input = //GETTING INPUT FROM THE USER
text= text.replaceAll(input,"<font color='red'>"+input+"</font>");
Textview.setText(Html.fromHtml(text));
And the replacement is working, but the problem is that my current code changes the original word cases, for example :
Text: HeLLo stackoverflow
Input: hello
What i get: <font color='red'>hello</font> stackoverflow
What i want: <font color='red'>HeLLo</font> stackoverflow
You have to think about regular expressions.
replaceAll allows you to use regular expressions, and so, you can replace the text for the exact occurrence that was found.
For instance if Hello was found, it replaces it for <font color='red'>Hello</font>.
If HeLLo is found, it replaces it for <font color='red'>HeLLo</font>
Your code should be somehing as easy as:
String highlighted = text.replaceAll("(?i)("+input+")","<font color='red'>$1</font>");
This means:
(?i) : i want to search for something, case insensitive
"("+input+")" : input is betwen ( and ) because we are creating a group, so this group can be refered later
"<font color='red'>$1</font>" : instead of replacing by input, that would change the case, we replace it by `$1, that is the reference to the first matched group. This means that we want to replace it using the exact word that was found.
But please, try it and keep playing since regular expressions are tricky.
Other reads
It is easier and more clear if you use the Patternclass.
You can read more here:
http://developer.android.com/reference/java/util/regex/Pattern.html
Also, you can take a look at how to do it:
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29
public String replaceAll(String regex, String replacement)
.
Replaces each substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)
Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.
Parameters:
regex - the regular expression to which this string is to be matched
replacement - the string to be substituted for each match
Returns:
The resulting String
UPDATE
You can test your regular expressions in this page:
http://www.regexplanet.com/advanced/java/index.html

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

How to use an approximate string matching algorithm in android sqlite?

i want to create an android application that can search word in sqlite database using approximate string matching.
for example if someone misspelled the word "switch" with " swithc", the sistem will correct the word and show message "did you mean 'switch' ".
its like google that can correct wrong word. how can i do it ?
have a look at this answer
Java: how to find the most probable string in a list of strings?
you can use the way of string matching as you desire. there is also a github project for this at:
https://github.com/northpoint/blur

How to search and Highlight Text within an EditText

I've searched high and low for something that seems to be a simple task. Forgive me, I am coming to Android from other programming languages and am new to this platform and Java.
What I want to do is create a dialog pop-up where a user enters text to search for and the code would take that text and search for it within all the text in an EditText control and if it's found, highlight it.
I've done this before, for example in VB and it went something similar to this pseudo code:
grab the text from the (EditText) assign it to a string
search the length of that string (character by character) for the substring, if it's found return the position (index) of the substring within the string.
if found, start the (EditText).setSelection highlight beginning on the returned position for the length of
Does this make sense?
I just want to search a EditText for and when found, scroll to it and it'll be highlighted. Maybe there's something in Android/Java equivalent to what I need here?
Any help / pointers would be greatly appreciated
grab the text from the (EditText) assign it to a string
Try the code sample below:
EditText inputUsername = (EditText) findViewById(R.id.manageAccountInputUsername);
inputUsername.getText().toString()
^^ Replace the IDs with the IDs you are using.
After this, you have the standard string methods available. You could also use Regex for a complex search query:
http://developer.android.com/reference/java/lang/String.html
http://developer.android.com/reference/java/util/regex/package-summary.html

Categories

Resources