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
Related
I want to put extra value from intent to other intent. But in other intent, app get all value. Example:
mAddress.setText(" from " + address);
String put_address = mAddress.getText().toString();
editIntent.putExtra("put_address", put_address);
is it possible to cut text "from" and get only address variable ???
you can split a string like
str = "From address#dd.com";
String modified = str.replace;
now splitstr contain your split strings
splitStr[1] contains "address#dd.com"
Can also use
str.substring(str.indexOf(" ")+1);
By the way, you can use jagapathi's answer. In his example he uses regular expression.
Regular expressions can help to parse, find, cut substrings using a particular pattern. In his code he splits string by any space character.
But, imho, the simplest solution is to create a substring using this code:
'put_address.substring(7);'
use one of these solutions:
String input = put_address.trim().substring(5);
*** note: 5 is index of real address first character;
String input = put_address..split(" ")[1];
I want to use split method to find special characters and then remove them and replace with images. I used html formatted texts in CDATA tag in Strings.xml file and send it to a Textview . How can I determine that special characters in that text (html formatted) in my java code and replace images and show those images between texts.
Thanks.
The simplest way would be to search within the String using the indexOf method. Something like this:
String yourString = "lorem(ipsum)";
String [] charsToReplace = new Array ('(', ')');
for (String thisChar : charsToReplace) {
while (yourString.indexOf(thisChar) > -1) {
// do something with ImageSpan or something
}
}
Not sure if this is the best way though...
Due to HTML usage within a string resource, I can't convert this to string from a charsequence (I will lose the formatting otherwise).
<string name="exponent_key">x<sup><small>y</small></sup>/string>
After using getString() I want to replace the 'y' with 'other stuff' but how do you do that? It seems like a simple question but for some reason I can't find anything about it.
Edit: Now that I think about it, can I convert the charsequence to a string that contains the HTML code, and then convert it back to a charsequence later?
Edit: Forgot to mention that the string gets set to a button title, and then retrieved (where it is then used).
There is. Create a function where, as a parameter, you take a string that needs to be formatted. And in function, you just take it through itterator, and after that Html.fromHtml()
in your string.xml
<string name="exponent_key">x<sup><small>%1$d</small></sup></string>
in your code
textView.setText(getString(R.string.exponent_key,2))
Let's break down your question in multiple steps:
Replacing the y with "other stuff" can be done like this:
String.format("%1$", "otherStuff");
If you use getString(), you can do the same thing like that:
<string name="exponent_key">%1$</string>
---
String string = getString(R.string.exponent_key, "otherStuff");
For more than one element, do this way:
you can do that like this:
<string name="string_name">%1$ : %2$</string>
---
getString(R.string.string_name, new String[]{"hello", "world"});
In XML you cannot nest HTML code, since HTML is another type of XML and the parser messes up and cannot recognize what tags are for Android and what are for HTML. But.. there's a trick. You can do that in this way:
<string name="exponent_key"><![CDATA[x<sup><small>%1$</small>/sup>]]></string>
So, with the string above in your XML, just use and you're fine:
getString(R.string.exponent_key, "otherStuff");
Note: if you need to show the HTML in a TextView, just use Html.fromHtml:
textView.setText(Html.fromHtml(getString(R.string.exponent_key, "otherStuff")));
Consider the effect you want to achieve.
you can do like this.
SpannableString ss = new SpannableString("2");
// set superscript
ss.setSpan(new SuperscriptSpan(),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set font size
ss.setSpan(new AbsoluteSizeSpan(12,true),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(ss);
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)
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.