How can I add a backspace to a textView Android - android

I read the oracle escape sequence and realize that if I want to make a backspace in a textView I need to use "\b", the same way as we do for inserting a new line (/n). I've tried this line of code:
textView.setText("Hellos\bWorld");
Then, when I run the app, the textView shows this:
Hellos World
Intead of what I expected:
HelloWorld
I wish you can help me, how I can make a backspace within a textView. Any suggestion will be welcome.

Simplest method remove space in string is replace() method. It accepts two arguments 1st what word/char you want to search in string and 2nd what you want to replace with.
String dummy = "Hellos World";
String newText = dummy.replace("s ","");
textView.setText(newText);
//output > HelloWorld

String regex = "\\s*\\bis\\b\\s*";
String str = "Hellos World";
str = str.replaceAll(regex, "");
textView.setText(str);

\b doesn't work the way you are thinking. Basically,
\b allows you to perform a "whole words only". It matches the empty string at the beginning or end of a word.
So you can match \bword\b with \b. So to remove the character you want you either need to use substring or replace particular character.

Related

is it possible to cut text from string like this?

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];

Hide only part of text in TextView

Good Day i want to hide some specified or certain part of text in textview!Important: Im not talking about hide the full textview with TextView.setVisibility(View.Gone) I'm not talking about transparent of TEXT in textview!im not talking about hiding full text in textview!So please help me to hide some text.
Example: lets say i have a textview with this text (10-Sporting Goods)
I want to hide the (10-) and show only Sporting Goods text.Any help will be appreciated!Thank you very much beforehand!
Although even i would appreciate for your case to strongly go with DroidWorm/Gabriella approach , just for the information of all the other folks who may see this in future.
If you really wish to hide just a portion of your textview which has the entire string in itself, you should use a SpannableString , as below:-
tvHello = (TextView) findViewById(R.id.tvHello);
SpannableString customText = new SpannableString("10-Sporting Good");
customText.setSpan(new RelativeSizeSpan(.1f), 0, 3, 0);
tvHello.setText(customText);
This code will technically HIDE the 10- from 10-Sporting Good without using a substring.
You could try to get the whole text like
String text = textView.getText().toString();
and then make substring of it like this:
String wantedSubstr = text.substring(4); //for example - everything from the 4th index to the end
then set this substring as text of your textView like this:
textView.setText(wantedSubstr);
There is one the possible solution of it is that..First you have to find the index(position) of "-" and than split the string according to it therefore use below code
String text = textView.getText().toString();
int position=text.indexOf('-');
String wantedSubstr = text.substring(position+1);
textView.setText(wantedSubstr);
Will there always been "10_" in front of it? Or will there always be 3 characters before the text you want? Or will there always be a "-" or "_" before the text you want?
If so, you could just do a simple method which takes the substring and then updates the textview. If so I can help you write a simple method
You cannot hide part of textView, instead you can make a substring of the specific string and setText using it.
Do it like:
String originalString = "10-Sporting Goods";
String subString = originalString.substring(3);
textView.setText(asubstring);

Escape sequence string processing in android

I have a string variable in my android ap which contains some url http://www.google.com. The string variable is declared as shown below:
String html="";
But there is an error saying that I have to put (semicolon); after the a href=". But actually I want the entire line content ie
(href="http://www.google.com") in the string variable. How can it be achieved?? Thanks in advance.
doubles quotes needs to escaped.
try below one
String html="<a href='http://www.google.com'></a>";
else
String html= "";
You need to escape every " with \ if you want to put it in one String.
For example:
String html="";
If you don't do this, compiler assumes, that you end this String after href= and tries to understand what http://www.google.com means in Java ;)

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

Editable text to string

How can I convert editable text into string in Android ? Any solution?
If I understand correctly, you want to get the String of an Editable object, right? If yes, try using toString().
Based on this code (which you provided in response to Alex's answer):
Editable newTxt=(Editable)userName1.getText();
String newString = newTxt.toString();
It looks like you're trying to get the text out of a TextView or EditText. If that's the case then this should work:
String newString = userName1.getText().toString();
This code work correctly only when u put into button click because at that time user put values into editable text and then when user clicks button it fetch the data and convert into string
EditText dob=(EditText)findviewbyid(R.id.edit_id);
String str=dob.getText().toString();

Categories

Resources