When using the string classs trim method, which of the following cannot be trimmed?

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.

The string trim() method doesn't omit middle spaces.

Signature

The signature or syntax of the String class trim() method is given below:

Returns

string with omitted leading and trailing spaces

Internal implementation

Java String trim() Method Example

FileName: StringTrimExample.java

Test it Now

Output

hello string   javatpoint
hello stringjavatpoint   

Java String trim() Method Example 2

The 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 3

The 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 4

Since 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 Descriptionstatic String[] addStringToArray(String[] array, String str)

Append the given String to the given String array, returning a new array consisting of the input array contents plus the given String.

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 String array into a comma delimited String (i.e., CSV).

static String arrayToDelimitedString(Object[] arr, String delim)

Convert a String array into a delimited String (e.g.

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 Collection into a delimited String (e.g., CSV).

static String collectionToDelimitedString(Collection<?> coll, String delim)

Convert a Collection into a delimited String (e.g.

static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix)

Convert a Collection to a delimited String (e.g.

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 String arrays into one, with overlapping array elements included twice.

static boolean containsWhitespace(CharSequence str)

Check whether the given CharSequence contains any whitespace characters.

static boolean containsWhitespace(String str)

Check whether the given String contains any whitespace characters.

static int countOccurrencesOf(String str, String sub)

Count the occurrences of the substring sub in string str.

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 String.

static String[] delimitedListToStringArray(String str, String delimiter)

Take a String that is a delimited list and convert it into a String array.

static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete)

Take a String that is a delimited list and convert it into a String array.

static boolean endsWithIgnoreCase(String str, String suffix)

Test if the given String ends with the specified suffix, ignoring upper/lower case.

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 CharSequence is neither null nor of length 0.

static boolean hasLength(String str)

Check that the given String is neither null nor of length 0.

static boolean hasText(CharSequence str)

Check whether the given CharSequence contains actual text.

static boolean hasText(String str)

Check whether the given String contains actual text.

static boolean isEmpty(Object str) static boolean matchesCharacter(String str, char singleCharacter)

Test if the given String matches the given single character.

static String[] mergeStringArrays(String[] array1, String[] array2) static Locale parseLocale(String localeValue) static Locale parseLocaleString(String localeString)

Parse the given String representation into a Locale.

static TimeZone parseTimeZoneString(String timeZoneString)

Parse the given timeZoneString value into a TimeZone.

static boolean pathEquals(String path2, String path2)

Compare two paths after normalization of them.

static String quote(String str)

Quote the given String with single quotes.

static Object quoteIfString(Object obj)

Turn the given Object into a String with single quotes if it is a String; keeping the Object as-is else.

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 String array if necessary.

static String[] split(String toSplit, String delimiter)

Split a String at the first occurrence of the delimiter.

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 String starts with the specified prefix, ignoring upper/lower case.

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 String into a String array via a StringTokenizer.

static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)

Tokenize the given String into a String array via a StringTokenizer.

static String toLanguageTag(Locale locale) static String[] toStringArray(Collection<String> collection)

Copy the given Collection into a String array.

static String[] toStringArray(Enumeration<String> enumeration)

Copy the given Enumeration into a String array.

static CharSequence trimAllWhitespace(CharSequence text)

Trim all whitespace from the given CharSequence: leading, trailing, and in between characters.

static String trimAllWhitespace(String str)

Trim all whitespace from the given String: leading, trailing, and in between characters.

static String[] trimArrayElements(String[] array)

Trim the elements of the given String array, calling String.trim() on each non-null element.

static String trimLeadingCharacter(String str, char leadingCharacter)

Trim all occurrences of the supplied leading character from the given String.

static String trimLeadingWhitespace(String str)

Trim leading whitespace from the given String.

static String trimTrailingCharacter(String str, char trailingCharacter)

Trim all occurrences of the supplied trailing character from the given String.

static String trimTrailingWhitespace(String str)

Trim trailing whitespace from the given String.

static String trimWhitespace(String str)

Trim leading and trailing whitespace from the given String.

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.