简体   繁体   中英

How can I add a backspace to a textView 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM