Karthikeya Boyini has Published 2383 Articles

Remove newline, space and tab characters from a string in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 06:36:38

16K+ Views

To remove newline, space and tab characters from a string, replace them with empty as shown below.replaceAll("[\t ]", "");Above, the new line, tab, and space will get replaced with empty, since we have used replaceAll()The following is the complete example.Example Live Demopublic class Demo {    public static void main(String[] args) ... Read More

Java Program to check if the String contains any character in the given set of characters

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 06:34:27

588 Views

Here, we have a string.String str = "abcde";In the above string, we want to search for the following set of characters.// set of characters to be searched char[] chSearch = {'b', 'c'};For this, loop through the length of the string and check every character in the string str. If the ... Read More

Join Strings in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 06:32:26

1K+ Views

To join strings in Java, use the String.join() method. The delimiter set as the first parameter in the method is copied for each element.Let’s say we want to join the strings “Demo” and “Text”. With that, we want to set a delimeter $. For that, use the join() method as ... Read More

Format Calendar with String.format() in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 06:16:23

719 Views

Firstly, consider an object with date value.Object arrObj[] = { "Date", Calendar.getInstance() };After that, use the String.format() method to format Calendar and display the date.The following is an example.Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String []args){       Object arrObj[] = { "Date", Calendar.getInstance() ... Read More

Conversion characters for time in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 06:13:12

205 Views

The following are the conversion characters for date-time.CharacterDescriptioncComplete date and timeFISO 8601 dateDU.S. formatted date (month/day/year)T24-hour timer12-hour timeR24-hour time, no secondsYFour-digit year (with leading zeroes)yLast two digits of the year (with leading zeroes)CFirst two digits of the year (with leading zeroes)BFull month namebAbbreviated month namemTwo-digit month (with leading zeroes)dTwo-digit day ... Read More

Check that the String does not contain certain characters in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 06:09:03

3K+ Views

Let’s say the following is our string with special characters.String str = "test*$demo";Check for the special characters.Pattern pattern = Pattern.compile("[^A-Za-z0-9]"); Matcher match = pattern.matcher(str); boolean val = match.find();Now, if the bool value “val” is true, that would mean the special characters are in the string.if (val == true) System.out.println("Special characters ... Read More

Delete all whitespaces from a String in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 06:01:21

102 Views

To delete all whitespaces from a string, use the replaceAll() method and replace every whitespace with empty.Let’s say the following is our string.String str = "This is it!";Now, let us replace the whitespaces that will eventually delete them.String res = str.replaceAll("\s+", "");Example Live Demopublic class Demo {    public static void ... Read More

Argument Index in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 05:51:16

440 Views

Argument indices allow programmers to reorder the output. Let us see an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.printf("Before reordering = %s %s %s %s %s %s", "one", "two", "three", "four", "five", "six" );       System.out.printf("After reordering = %6$s ... Read More

Creating String Object from certain part of a character Array in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 05:49:54

79 Views

Here is our character array.char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};Create string object from some part of a string using the following String constructor. Through this we are fetching substring “IN” from the character array.String str = new String(ch, 4, 2);Example Live Demopublic class Demo {   ... Read More

Extracting a substring as an array of characters in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 05:47:28

3K+ Views

To extract a substring as an array of characters in Java, use the getChars() method.Let’s say the following is our string and character array.String str = "World is not enough!"; char[] chArr = new char[10];Now, use the getChars() method to extract a substring.str.getChars(13, 19, chArr, 0);The above substring is an ... Read More

Advertisements