TextUtils tool class

TextUtils class
First look at the construction method and find that its construction method is private and cannot be created by new. The general tool class provides some single methods. The general methods are decorated with static, so it is meaningless to create new. This is also the writing of general tools.

getChars(CharSequence s, int start, int end, char[] dest, int destoff){}

indexOf(CharSequence s, char ch)
indexOf(CharSequence s, char ch, int start)
indexOf(CharSequence s, char ch, int start, int end)
lastIndexOf(CharSequence s, char ch)
lastIndexOf(CharSequence s, char ch, int last)
lastIndexOf(CharSequence s, char ch, int start, int last)
indexOf(CharSequence s, CharSequence needle)
indexOf(CharSequence s, CharSequence needle, int start)
indexOf(CharSequence s, CharSequence needle, int start, int end)

// Return true if the string one matches the specified area of ​​two, otherwise false
// The internal implementation merges the parts of the two strings that need to be compared into a char [] and compares them before and after
// one: matches the string
// two: specify the string
// toffset / ooffset: the offset of the string
// len: the matched length
regionMatches (CharSequence one, int toffset, CharSequence two, int ooffset, int len)

// Intercept part of the original string and return
// Internal implementation If it is String, StringBuffer, StringBuilder, call their substring () method,
// Otherwise , put the part of the original string that needs to be intercepted into a char [] In, then converted to String
// source: original string
// start: start offset
// end: end offset
String substring (CharSequence source, int start, int end)

// Add a delimiter between the elements in an array, and finally return a String
// This algorithm very cleverly solves the problem of input problems before and after, I hope to see the source code to understand
// delimiter: what needs to be inserted
/// tokens: original array, collection cursor
String join (CharSequence delimiter, Object [] tokens)
String join (CharSequence delimiter, Iterable tokens)

// Split the string into a string array with another string as the match
// text: original string
// expression: matched string, regular
String [] split (String text, String expression)
String [] split (String text, Pattern pattern)

// Get String or SpannedString
CharSequence stringOrSpannedString (CharSequence source)

// judge empty
// return true
// str == null || str.length () == 0
boolean isEmpty (@Nullable CharSequence str)

// Get the length of the string excluding the first space
// Internally get the length of the space from the front and back respectively, and then subtract the length of the space from the total length
int getTrimmedLength (CharSequence s)

// Compare two strings
// Internally, if two ==, return directly, and then determine whether it is empty and the length is equal,
// Then determine if it is of type String, call String.equals (), otherwise separately charAt () for comparison
boolean equals (CharSequence a, CharSequence b)

// This is a Deprecated marked class
// Reverse a part of the word CharSequence
CharSequence getReverse (CharSequence source, int start, int end)

// Replacement function, the sources appearing in the template are replaced with destinations. Note: Only the first occurrence will be replaced.
CharSequence replace (CharSequence template, String [] sources, CharSequence [] destinations)

// Replace ^ 1, ^ 2, etc. in the template with the corresponding values ​​in the values. Note: No more than 9, more than 9 will throw an exception.
CharSequence expandTemplate (CharSequence template, CharSequence… values)
// Example:
String template = “This is a ^ 1 of the ^ 2 broadcast ^ 3.”;
CharSequence expandTemplate = TextUtils.expandTemplate (template, “test”, “emergency”, "System");

// 将CharSequence写入Parcel
writeToParcel(CharSequence cs, Parcel p, int parcelableFlags)

// Print Span in cs, the prefix will appear after printing each Span. Printer, you can use LogPrinter, so you can print in the log.
dumpSpans (CharSequence cs, Printer printer, String prefix)

getOffsetBefore()/getOffsetAfter()

// Get the offset before or after the
text int getOffsetBefore (CharSequence text, int offset)
int getOffsetAfter (CharSequence text, int offset)
copySpansFrom ()

// Copy the span from start to end in source to dest, destoff is the offset.
copySpansFrom (Spanned source, int start, int end, Class kind, Spannable dest, int destoff)
ellipsize ()

// Equivalent to ellipsize in TextView ’s xml. Here you can call back the index of the omitted range.
// At the same time, you can use preserveLength to set the length of the returned CharSequence to the original length or the length after the omission.
// The "zero width" used here is not "
Newline space character" to occupy the place, where parameters can be used // TextUtils.TruncateAt.START, TextUtils.TruncateAt.END, TextUtils.TruncateAt.MIDDLE and TextUtils.TruncateAt.MARQUEE,
// are the beginning, end, middle and marquee .
CharSequence ellipsize (CharSequence text, TextPaint p, float avail, TruncateAt where)
CharSequence ellipsize (CharSequence text, TextPaint paint, float avail, TruncateAt where, boolean preserveLength, EllipsizeCallback callback)
CharSequence ellipsize (CharSequence text, TextPaint paint, float avail , boolean preserveLength, EllipsizeCallback callback, TextDirectionHeuristic textDir, String ellipsis)
htmlEncode ()

// encode html
String htmlEncode(String s)
concat()

// text is a variable parameter, you can combine the incoming CharSequence into a CharSequence, and retain the original Span.
CharSequence concat (CharSequence… text)
isGraphic ()

// Determine whether there are characters that can be displayed
boolean isGraphic (CharSequence str)
isDigitsOnly ()

// Determine if there are only numbers
boolean isDigitsOnly (CharSequence str)

Published 16 original articles · Likes0 · Visits 850

Guess you like

Origin blog.csdn.net/weixin_45830683/article/details/103037168