Karthikeya Boyini has Published 2193 Articles

Get the substring before the last occurrence of a separator in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 06:46:18

6K+ Views

We have the following string with a separator.String str = "David-Warner";We want the substring before the last occurrence of a separator. Use the lastIndexOf() method.For that, you need to get the index of the separator using indexOf()String separator ="-"; int sepPos = str.lastIndexOf(separator); System.out.println("Substring before last separator = "+str.substring(0, sepPos));The ... Read More

Line Separator in Java

karthikeya Boyini

karthikeya Boyini

Updated on 27-Jun-2020 06:40:15

3K+ Views

Strings have no newlines. We can form them into two lines by concatenating a newline string. Use System lineSeparator to get a platform-dependent newline string.The following is an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "one" + System.lineSeparator() + ... Read More

Display Month in MMM format in Java

karthikeya Boyini

karthikeya Boyini

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

5K+ Views

The MMM format for months is the short name i.e. Jan, Feb, Mar, Apr, etc. Here, we will use the following.SimpleDateFormat("MMM");Let us see an example.// displaying month in MMM format SimpleDateFormat simpleformat = new SimpleDateFormat("MMM"); String strMonth= simpleformat.format(new Date()); System.out.println("Month in MMM format = "+strMonth);Above, we have used the SimpleDateFormat ... Read More

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

karthikeya Boyini

karthikeya Boyini

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

18K+ 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

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

948 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

275 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

4K+ 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

166 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

608 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

Advertisements