The Java String class trim() method eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in Java string checks this Unicode value before and after the string, if it exists then the method removes the spaces and returns the omitted string. Show
The string trim() method doesn't omit middle spaces.SignatureThe signature or syntax of the String class trim() method is given below: Returnsstring with omitted leading and trailing spaces Internal implementationJava String trim() Method ExampleFileName: StringTrimExample.java Test it NowOutput hello string javatpoint hello stringjavatpoint Java String trim() Method Example 2The example demonstrates the use of the trim() method. This method removes all the trailing spaces so the length of the string also reduces. Let's see an example. FileName: StringTrimExample2.java Output 22 hello java string 17 hello java string Java String trim() Method Example 3The trim() can be used to check whether the string only contains white spaces or not. The following example shows the same. FileName: TrimExample3.java Output The string contains characters other than white spaces The string contains only white spaces Java String trim() Method Example 4Since strings in Java are immutable; therefore, when the trim() method manipulates the string by trimming the whitespaces, it returns a new string. If the manipulation is not done by the trim() method, then the reference of the same string is returned. Observe the following example. FileName: TrimExample4.java Output The string contains characters other than white spaces The string contains only white spaces All Methods Static Methods Concrete Methods Deprecated Methods Modifier and TypeMethod and Description Append the given static String
applyRelativePath(String path,
String relativePath) Apply the given relative path to the given Java resource path, assuming standard Java folder separation (i.e. static String
arrayToCommaDelimitedString(Object[] arr) Convert a static String
arrayToDelimitedString(Object[] arr,
String delim) Convert a static String
capitalize(String str)
static String
cleanPath(String path) Normalize the path by suppressing sequences like "path/.." and inner simple dots. static String
collectionToCommaDelimitedString(Collection<?> coll) Convert a static String
collectionToDelimitedString(Collection<?> coll,
String delim) Convert a static String
collectionToDelimitedString(Collection<?> coll,
String delim,
String prefix,
String suffix) Convert a static Set<String>
commaDelimitedListToSet(String str) Convert a comma delimited list (e.g., a row from a CSV file) into a set. static String[]
commaDelimitedListToStringArray(String str) Convert a comma delimited list (e.g., a row from a CSV file) into an array of strings. static String[]
concatenateStringArrays(String[] array1,
String[] array2) Concatenate the given static boolean
containsWhitespace(CharSequence str) Check whether the given static boolean
containsWhitespace(String str) Check whether the given static int
countOccurrencesOf(String str,
String sub) Count the occurrences of the substring static String
delete(String inString,
String pattern) Delete all occurrences of the given substring. static String
deleteAny(String inString,
String charsToDelete) Delete any character in a given static String[]
delimitedListToStringArray(String str,
String delimiter) Take a static String[]
delimitedListToStringArray(String str,
String delimiter,
String charsToDelete) Take a static boolean
endsWithIgnoreCase(String str,
String suffix) Test if the given static String
getFilename(String path) Extract the filename from the given Java resource path, e.g. static String
getFilenameExtension(String path) Extract the filename extension from the given Java resource path, e.g. static boolean
hasLength(CharSequence str) Check that the given static boolean
hasLength(String str) Check that the given static boolean
hasText(CharSequence str) Check whether the given static boolean
hasText(String str) Check whether the given static boolean
isEmpty(Object str)
static boolean
matchesCharacter(String str,
char singleCharacter) Test if the given static String[]
mergeStringArrays(String[] array1,
String[] array2)
static Locale
parseLocale(String localeValue)
static Locale
parseLocaleString(String localeString) Parse the given static TimeZone
parseTimeZoneString(String timeZoneString) Parse the given static boolean
pathEquals(String path2,
String path2) Compare two paths after normalization of them. static String
quote(String str) Quote the given static Object
quoteIfString(Object obj) Turn the given Object into a static String[]
removeDuplicateStrings(String[] array) Remove duplicate strings from the given array. static String
replace(String inString,
String oldPattern,
String newPattern) Replace all occurrences of a substring within a string with another string. static String[]
sortStringArray(String[] array) Sort the given static String[]
split(String toSplit,
String delimiter) Split a static Properties
splitArrayElementsIntoProperties(String[] array,
String delimiter) Take an array of strings and split each element based on the given delimiter. static Properties
splitArrayElementsIntoProperties(String[] array,
String delimiter,
String charsToDelete) Take an array of strings and split each element based on the given delimiter. static boolean
startsWithIgnoreCase(String str,
String prefix) Test if the given static String
stripFilenameExtension(String path) Strip the filename extension from the given Java resource path, e.g. static boolean
substringMatch(CharSequence str,
int index,
CharSequence substring) Test whether the given string matches the given substring at the given index. static String[]
tokenizeToStringArray(String str,
String delimiters) Tokenize the given static String[]
tokenizeToStringArray(String str,
String delimiters,
boolean trimTokens,
boolean ignoreEmptyTokens) Tokenize the given static String
toLanguageTag(Locale locale)
static String[]
toStringArray(Collection<String> collection) Copy the given static String[]
toStringArray(Enumeration<String> enumeration) Copy the given static CharSequence
trimAllWhitespace(CharSequence text) Trim all whitespace from the given static String
trimAllWhitespace(String str) Trim all whitespace from the given static String[]
trimArrayElements(String[] array) Trim the elements of the given static String
trimLeadingCharacter(String str,
char leadingCharacter) Trim all occurrences of the supplied leading character from the given static String
trimLeadingWhitespace(String str) Trim leading whitespace from the given static String
trimTrailingCharacter(String str,
char trailingCharacter) Trim all occurrences of the supplied trailing character from the given static String
trimTrailingWhitespace(String str) Trim trailing whitespace from the given static String
trimWhitespace(String str) Trim leading and trailing whitespace from the given static String
uncapitalize(String str)
static String
unqualify(String qualifiedName) Unqualify a string qualified by a '.' dot character. static String
unqualify(String qualifiedName,
char separator) Unqualify a string qualified by a separator character. static String
uriDecode(String source,
Charset charset) Decode the given encoded URI component value. Which methods of the String class can be used to tokenize a string?StringTokenizer(String str, String delim): delim is a set of delimiters that are used to tokenize the given string.
Is commonly used to refer to a string that is part of another string?Substrings. A substring is any sequence of characters that is contained in a string. Use the Substring method to create a new string from a part of the original string. You can search for one or more occurrences of a substring by using the IndexOf method.
What is the term used for a class that is wrapped around a primitive data type and allows you to create objects instead of primitive type variables?A wrapper class is a class that is "wrapped around" a primitive data type and allows you to create objects instead of variables. The wrapper classes in Java are immutable, which means that once you create an object, you cannot change the object's value.
What is the term used for the character that separates tokens?What is the term used for the character that separates tokens? delimiter.
|