Search code examples
androidtextutils

For what purpose is Android TextUtils used?


I was wondering - "why do others use TextUtils in many purposes?" - but I am not clear about this. The Developer site says that it is a simple string splitter. I understand this but I don't know how to use this in a practical manner or what purposes I can use it? Can anyone provide me some practical example with a code snippet?


Solution

  • It is simply a set of utility functions to do operations on String objects. In fact, the whole class doesn't have any instance fields or methods. Everything is static. Consider it like a container to group functions with a text-based semantic. Many of them could have been methods of a String inherited class or CharSequence inherited class. For example you can do:

    TextUtils.indexOf(string, char)
    

    which is the same of doing

    string.indexOf(char);
    

    Many of them do things that you can already do with string public methods. Many others add additional functionalities. This class serves at a method level the same purpose that a package serves at a class level.