How to Replace Spaces with Dashes in Java
We can use the Java String built-in `replace()` or `replaceAll()` methods to replace spaces with dashes (`-`) easily.
We can use the Java String built-in `replace()` or `replaceAll()` methods to replace spaces with dashes (`-`) easily.
The Java String compareTo() method compares two strings lexicographically (alphabetical order). It returns a positive number, negative number, or zero. s1.compareTo(s2) if s1 < s2, it returns negative number. if s1 > s2, it returns positive number. if s1 == s2, it returns 0. Table of contents 1. "a".compareTo("c"), negative integer 2. "c".compareTo("a"), positive integer …
In Java, we can use substring(0, 1).toUpperCase() + str.substring(1) to make the first letter of a String as a capital letter (uppercase letter) String str = "mkyong"; String str1 = str.substring(0, 1).toUpperCase(); // first letter = M String str2 = str.substring(1); // after 1 letter = kyong String result = str.substring(0, 1).toUpperCase() + str.substring(1); // …
In Java, we can use Integer.valueOf(String) to convert a String to an Integer object; For unparsable String, it throws NumberFormatException. Integer.valueOf("1"); // ok Integer.valueOf("+1"); // ok, result = 1 Integer.valueOf("-1"); // ok, result = -1 Integer.valueOf("100"); // ok Integer.valueOf(" 1"); // NumberFormatException (contains space) Integer.valueOf("1 "); // NumberFormatException (contains space) Integer.valueOf("2147483648"); // NumberFormatException (Integer max …
In Java, we use equals() to compare string value. String str1 = "apple"; // compare strings case-sensitive if (str1.equals("apple")) { System.out.println("I have an apple."); } String str2 = "apple"; // compare strings case-insensitive if (str2.equalsIgnoreCase("APPLE")) { System.out.println("I have an APPLE."); } Output Terminal I have an apple. I have an APPLE. In this article, we …
In Java, we can use String#split() to split a string. String phone = "012-3456789"; // String#split(string regex) accepts regex as the argument String[] output = phone.split("-"); String part1 = output[0]; // 012 String part2 = output[1]; // 3456789 Table of contents 1. Split a string (default) 2. Split a string and retain the delimiter 3. …
In Java, we can use Integer.parseInt(String) to convert a String to an int; For unparsable String, it throws NumberFormatException. Integer.parseInt("1"); // ok Integer.parseInt("+1"); // ok, result = 1 Integer.parseInt("-1"); // ok, result = -1 Integer.parseInt("100"); // ok Integer.parseInt(" 1"); // NumberFormatException (contains space) Integer.parseInt("1 "); // NumberFormatException (contains space) Integer.parseInt("2147483648"); // NumberFormatException (Integer max 2,147,483,647) …
This article shows a few ways to convert an java.io.InputStream to a String. Table of contents 1. ByteArrayOutputStream 2. InputStream#readAllBytes (Java 9) 3. InputStreamReader + StringBuilder 4. InputStreamReader + BufferedReader (modified line breaks) 5. Java 8 BufferedReader#lines (modified line breaks) 6. Apache Commons IO 7. Download Source Code 8. References What are modified line breaks? …
In Java, we can use ByteArrayInputStream to convert a String to an InputStream. String str = "mkyong.com"; InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)); Table of contents 1. ByteArrayInputStream 2. Apache Commons IO – IOUtils 3. Download Source Code 4. References 1. ByteArrayInputStream This example uses ByteArrayInputStream to convert a String to an InputStream and saves it …
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); …
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 …