Karthikeya Boyini has Published 2193 Articles

Convert a String to a short number in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 16:21:34

298 Views

Use the Short.parseShort() method in Java to convert a String to a short.Let’s say the following is our string.String str = “76”;Now, the parseShort() method will convert the string to short.byte val = Byte.parseByte(str);Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "76";        short val = Short.parseShort(str);        System.out.println(val); ... Read More

Convert string of time to time object in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 15:31:06

10K+ Views

Here is our string.String strTime = "20:15:40";Now, use the DateFormat to set the format for date.DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");Parse the string of time to time object.Date d = dateFormat.parse(strTime);The following is the complete example.Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.text.SimpleDateFormat; public class Demo {     public static void main(String[] args) throws Exception {        String strTime = "20:15:40";        DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); ... Read More

Finding the last occurrence of a character in a String in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 15:12:51

2K+ Views

Use the lastIndexOf() method to find the last occurrence of a character in a string in Java.Let’s say the following is our string.String myStr = "Amit Diwan";In the above string, we will find the last occurrence of character ‘i’myStr.lastIndexOf('i');The following is the complete example.Example Live Demopublic class Demo {  public static void main(String[] args) {     String myStr = "Amit Diwan"; ... Read More

Java Program to search for a Substring from a specified index

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 15:08:13

418 Views

Use the indexOf() method to search for a substring from a given position.Let’s say the following is our string.String myStr = " pqrstuvwxyzpqrst";Searching for the substring “pqrs” in the string. We are beginning the index from 3 for our search.int begnIndex = 3; strLastIndex = myStr.indexOf("pqrs", begnIndex);Example Live Demopublic class Demo {     ... Read More

Display the Operating System name in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 15:01:44

4K+ Views

Use the System.getProperty() method in Java to get the Operating System name.It’s syntax is −String getProperty(String key)Above, the key is the name of the system property. Since, we want the OS name, therefore we will add the key as −os.nameExample Live Demopublic class Demo {     public static void main(String[] args) {        System.out.print("Operating System: "); ... Read More

Tokenizing a String in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 14:58:41

330 Views

We have the following string −String str = "This is demo text, and demo line!";To tokenize the string, let us split them after every period (.) and comma (, )String str = "This is demo text, and demo line!";The following is the complete example.Example Live Demopublic class Demo {     public static void main(String[] args) {     ... Read More

Java Program to compare string using compareTo() method

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 14:51:24

167 Views

The compareTo(obj) method compares this String to another Object.The value 0 is returned if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is ... Read More

Replace first occurrence of a character in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 14:40:01

8K+ Views

To replace the first occurrence of a character in Java, use the replaceFirst() method.Here is our string.String str = "The Haunting of Hill House!";Let us replace the first occurrence of character “H”str.replaceFirst("(?:H)+", "B");The following is the complete example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "The Haunting of Hill House!"; ... Read More

Concatenate null to a string in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 14:36:18

3K+ Views

To concatenate null to a string, use the + operator.Let’s say the following is our string.String str = "Demo Text";We will now see how to effortlessly concatenate null to string.String strNULL = str + "null";The following is the final example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "Demo Text";   ... Read More

Java Program to compare strings for equality

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 14:31:46

243 Views

To compare string for equality in Java, use the equals() method. Let us see some examples wherein we have checked for same as well as different string values.Example Live Demopublic class Demo {     public static void main(String[] args) {        String one = "x";        String two = "x";        if(one.equals(two)) {           ... Read More

Advertisements