How-to articles, tricks, and solutions about STRING

Remove control characters from PHP string

In PHP, you can use the preg_replace function to remove control characters from a string.

Remove empty strings from a list of strings

This code uses a list comprehension to iterate through the original list and only keep the elements that are not empty strings.

Remove excess whitespace from within a string

To remove excess whitespace from within a string in PHP, you can use the preg_replace() function with a regular expression that matches any sequence of one or more whitespace characters.

Remove final character from string

You can remove the final character from a string in Python using string slicing.

Remove new lines from string and replace with one empty space

To remove new lines from a string and replace them with a single empty space in PHP, you can use the str_replace function.

Remove part of string in Java

To remove a part of a string in Java, you can use the replace() method of the String class.

Remove portion of a string after a certain character

There are several ways you can remove a portion of a string after a certain character in PHP.

Remove specific characters from a string in Python

In Python, you can remove specific characters from a string by using the replace() method or using string slicing and concatenation.

Replace a character at a specific index in a string?

To replace a character at a specific index in a string in Java, you can use the substring() method of the java.lang.

Reverse a string in Java

To reverse a string in Java, you can use the following approaches:

search a php array for partial string match

You can use the array_filter() function in PHP to search through an array and return only the elements that contain a certain string.

Simple way to count character occurrences in a string

To count the occurrences of a character in a string in Java, you can use the charAt() method of the String class to iterate over the characters in the string and check if each character is equal to the target character.

Simple way to repeat a string

In Java, you can use the String.repeat() method to repeat a string multiple times.

Split string on whitespace in Python

You can use the split() method to split a string on whitespace in Python.

Split string with multiple delimiters in Python

You can use the re.split() function from the re module in Python to split a string using multiple delimiters.

String concatenation: concat() vs "+" operator

In Java, you can use the concat() method of the java.lang.String class or the + operator to concatenate (join) two or more strings.

string to string array conversion in java

There are several ways to convert a string to a string array in Java. Here are a few options:

String.equals versus ==

In Java, the == operator is used to compare the primitive values of two variables, while the equals() method is used to compare the contents of two objects.

StringBuilder vs String concatenation in toString() in Java

In Java, you can use either a StringBuilder or string concatenation to create a string representation of an object.

Strip Leading and Trailing Spaces From Java String

To remove leading and trailing whitespace from a string in Java, you can use the trim method of the String class.

The “right” JSON Date Format

Read the tutorial and learn the right methods of JSON date formatting with the help of JavaScript. Get to know information about ISO 8601 string format.

Truncate a string to first n characters of a string and add three dots if any characters are removed

To truncate a string to the first n characters and add three dots if any characters are removed in PHP, you can use the following function.

TypeError: a bytes-like object is required, not 'str' when writing to a file in Python 3

This error occurs when you are trying to write a string to a file using the write() method in Python 3, but the file is opened in binary mode (using the 'b' flag when opening the file).

Using Enum values as String literals

To use the values of an enum as string literals in Java, you can use the name() method of the Enum class. This method returns the name of the enum value as a string.