Karthikeya Boyini has Published 2383 Articles

Java Program to set a range for displaying substring

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 16:10:40

1K+ Views

Use the substring() method to set a range of substring from a string. Let’s say the following is our string.String str = "pqrstuvw";Getting the range of string from 3 to 6String strRange = str.substring(3, 6);The following is the complete example wherein we have set a range for displaying substring from ... Read More

Java Program to convert a string into a numeric primitive type using Integer.valueOf()

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 15:33:54

455 Views

The Integer.valueOf() method is used in Java to convert a string into a numeric primitive type.Let’s say the following is our string.String str = "989";To convert it into Integer primitive type, use Integer.valueOf()Integer.valueOf(str)Let us see the complete example to convert a string to Integer primitive type.Example Live Demopublic class Demo {     public static void main(String[] args) { ... Read More

Convert string of time to time object in Java

karthikeya Boyini

karthikeya Boyini

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

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

278 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

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

270 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

97 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

Java Regex to extract maximum numeric value from a string

karthikeya Boyini

karthikeya Boyini

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

478 Views

The maximum numeric value is extracted from an alphanumeric string. An example of this is given as follows −String = abcd657efgh234 Maximum numeric value = 657A program that demonstrates this is given as follows −Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {  public static void main (String[] args) {     String str = "123abc874def235ijk999";     System.out.println("The string is: " + str);     ... 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

Advertisements