Java – How to display % in String.Format?

This example shows you how to display a percentage % in String.format. JavaStringFormat1.java package com.mkyong; public class JavaStringFormat1 { public static void main(String[] args) { String result = String.format("%d%", 100); System.out.println(result); } } Output Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = ‘%’ at java.base/java.util.Formatter.checkText(Formatter.java:2732) at java.base/java.util.Formatter.parse(Formatter.java:2718) at java.base/java.util.Formatter.format(Formatter.java:2655) at java.base/java.util.Formatter.format(Formatter.java:2609) at java.base/java.lang.String.format(String.java:2897) Solution To display …

Read more

How to pad a String in Java?

This article shows you how to use the JDK1.5 String.format() and Apache Common Lang to left or right pad a String in Java. 1. String.format By default, the String.format() fills extra with spaces \u0020. Normally, we use replace() to pad with other chars, but it will replace the spaces in between the given string. JavaPadString1.java …

Read more

Java String Format Examples

This article shows you how to format a string in Java, via String.format(). Here is the summary. Conversion Category Description %b, %B general true of false %h, %H general hash code value of the object %s, %S general string %c, %C character unicode character %d integral decimal integer %o integral octal integer, base 8 %x, …

Read more

Java – Add new line in String

Different operating system has a different new line or line separator string: UNIX, Linux or Mac OSX = \n Windows = \r\n NewLineExample.java package com.mkyong; public class NewLineExample { public static void main(String[] args) { String original = "Hello World Java"; System.out.println(original); // add new line String originalNewLine = "Hello\nWorld\nJava"; System.out.println(originalNewLine); } } Output Hello …

Read more

Java – Check if a String contains a substring

In Java, we can use String.contains() to check if a String contains a substring. 1. String.contains() – Case Sensitive JavaExample1.java package com.mkyong; public class JavaExample1 { public static void main(String[] args) { String name = "mkyong is learning Java 123"; System.out.println(name.contains("Java")); // true System.out.println(name.contains("java")); // false System.out.println(name.contains("MKYONG")); // false System.out.println(name.contains("mkyong")); // true if (name.contains("Java")) { …

Read more

Python – Check if a String contains another String?

In Python, we can use in operator or str.find() to check if a String contains another String. 1. in operator name = "mkyong is learning python 123" if "python" in name: print("found python!") else: print("nothing") Output found python! 2. str.find() name = "mkyong is learning python 123" if name.find("python") != -1: print("found python!") else: print("nothing") …

Read more

Java – String vs StringBuffer

This article shows the difference in time taken for similar operations on a String object and StringBuffer object. String being an immutable class, it instantiates a new object each time an operation is performed on it StringBuffer being a mutable class, the overhead of object instantiation during operations is removed. Hence, the time taken for …

Read more

Java – How to convert Char[] to String

In Java, we can use String.valueOf() to convert a char array to a String. JavaSample1.java package com.mkyong.markdown; public class JavaSample1 { public static void main(String[] args) { char[] charArrays = new char[]{‘1’, ‘2’, ‘3’, ‘A’, ‘B’, ‘C’}; String str = new String(charArrays); System.out.println(str); // 123ABC String str2 = String.valueOf(charArrays); System.out.println(str2); // 123ABC } } Output …

Read more

Java – How to convert String to Char Array

In Java, you can use String.toCharArray() to convert a String into a char array. StringToCharArray.java package com.mkyong.utils; public class StringToCharArray { public static void main(String[] args) { String password = "password123"; char[] passwordInCharArray = password.toCharArray(); for (char temp : passwordInCharArray) { System.out.println(temp); } } } Output p a s s w o r d 1 …

Read more

Java StringTokenizer examples

In Java, we use StringTokenizer to split a string into multiple tokens. Note The StringTokenizer is a legacy class, try the split method of String, read this How to split a string in Java. 1. StringTokenizer examples 1.1 By default, the StringTokenizer uses default delimiters: space, tab, newline, carriage-return, and form-feed characters to split a …

Read more

Java – How to convert Char to String

A Java example to show you how to convert a Char into a String and vise verse. ConvertCharToString.java package com.mkyong.utils; public class ConvertCharToString { public static void main(String[] args) { String website = "https://mkyong.com"; //convert a String into char char charH = website.charAt(0); //h char charP = website.charAt(3);//p char charM = website.charAt(11);//m System.out.println(charH); System.out.println(charP); System.out.println(charM); …

Read more

How to convert String to byte[] in Java?

In Java, we can use str.getBytes(StandardCharsets.UTF_8) to convert a String into a byte[]. String str = "This is a String"; // default charset, a bit dangerous byte[] output1 = str.getBytes(); // in old days, before java 1.7 byte[] output2 = str.getBytes(Charset.forName("UTF-8")); // the best , java 1.7+ , new class StandardCharsets byte[] output3 = str.getBytes(StandardCharsets.UTF_8); …

Read more

How to convert byte[] array to String in Java

In Java, we can use new String(bytes, StandardCharsets.UTF_8) to convert a byte[] to a String. // string to byte[] byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8); // byte[] to string String s = new String(bytes, StandardCharsets.UTF_8); Table of contents 1. byte[] in text and binary data 2. Convert byte[] to String (text data) 3. Convert byte[] to String …

Read more