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 Java – How to display % in String.Format?

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 – Add new line in String

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 Java – Check if a String contains a substring

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 Python – Check if a String contains another 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 Char[] to String

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 – How to convert String to Char Array

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 Java – How to convert Char to String

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 String to byte[] 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 How to convert byte[] array to String in Java