Found 7442 Articles for Java

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

karthikeya Boyini
Updated on 13-Nov-2024 19:19:45

852 Views

This article will teach us to convert a string into a numeric primitive type using Integer.valueOf() in Java. By the end, you’ll see how this method takes a string of numbers and transforms it into an integer type, making it usable for arithmetic operations or any other number-based logic. Problem Statement Write a Java program to convert a numeric string to an integer using the Integer.valueOf() method and display the result. Below is a demostration of the same − Input str = "989" Output 989 Steps to convert a string into a numeric primitive type Following are the steps to convert a ... Read More

Narrowing Conversion in Java

Samual Sam
Updated on 26-Jun-2020 16:09:51

3K+ Views

Narrowing conversion is needed when you convert from a larger size type to a smaller size. This is for incompatible data types, wherein automatic conversions cannot be done.Let us see an example wherein we are converting long to integer using Narrowing Conversion.Example Live Demopublic class Demo {     public static void main(String[] args) {        long longVal = 878;        int intVal = (int) longVal;        System.out.println("Long: "+longVal);        System.out.println("Integer: "+intVal);     } }OutputLong: 878 Integer: 878Let us see another example, wherein we are converting double to long using Narrowing Conversion.Example Live Demopublic class Demo {     public static void main(String[] args) {        double doubleVal = 299.89;        long longVal = (long)doubleVal;       ... Read More

Java program to set a range for displaying substring

karthikeya Boyini
Updated on 19-Aug-2024 18:33:51

2K+ Views

In this article, we'll learn how to extract a specific range of characters from a string in Java using the substring() method. The substring(int beginIndex, int endIndex) method gets a part of a string from the beginIndex to just before the endIndex. Problem Statement Given a string, extract a substring from a specified range of indices. Input String: pqrstuvw Output Substring: stu Steps to set a range for displaying substring Following are the steps to set a range for displaying substring − Declare a string str. Initialize the string str with the ... Read More

Java Program to search for last index of a group of characters

Samual Sam
Updated on 26-Jun-2020 16:15:37

193 Views

Use the lastIndexOf() method to search for last index of a group of characters. Let’s say the following is our string.String myStr = "pqrstuvwxyzpqrst";Searching for the substring “pqrs” in the string to get the last index of its occurring in the string. We begin the search from index 3.int begnIndex = 3; strLastIndex = myStr.indexOf("pqrs", begnIndex);The following is an example, wherein we are searching for the last index of the substring “pqrs”Example Live Demopublic class Demo {     public static void main(String[] args) {        String myStr = "pqrstuvwxyzpqrst";        int strLastIndex = 0;        System.out.println("String: "+myStr);        strLastIndex = myStr.lastIndexOf("pqrs");        System.out.println("The last index of ... Read More

Java Program to search for a Substring from a specified index

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 {     public static void main(String[] args) {        String myStr = "pqrstuvwxyzpqrst";        int strLastIndex = 0;        int begnIndex = 3;        System.out.println("String: "+myStr);        strLastIndex = myStr.indexOf("pqrs",  begnIndex);        System.out.println("The index of substring pqrs in the string beginning from index "+begnIndex+" = "+strLastIndex);     } }OutputString: pqrstuvwxyzpqrst The index of substring pqrs ... Read More

Search for a character from a given position in Java

Samual Sam
Updated on 26-Jun-2020 15:10:01

140 Views

Use the indexOf() method to search for a character from a given position.Let’s say the following is our string.String myStr = "Amit Diwan";Here, we are searching for the character “i” in the string. We are beginning the index from 4 for our search.int begnIndex = 4; System.out.println("String: "+myStr); strLastIndex = myStr.indexOf('i', begnIndex);The following is the complete example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String myStr = "Amit Diwan";       int strLastIndex = 0;       int begnIndex = 4;       System.out.println("String: "+myStr);       strLastIndex = myStr.indexOf('i',  begnIndex);       System.out.println("The index of character a in the string beginning from index "+begnIndex+" ="+strLastIndex);   ... Read More

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

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";     int strLastIndex = 0;     System.out.println("String: "+myStr);     strLastIndex = myStr.lastIndexOf('i');     System.out.println("The last index of character a in the string: "+strLastIndex);  } }OutputString: Amit Diwan The last index of character a in the string: 6Read More

Search index of a character in a string in Java

Samual Sam
Updated on 26-Jun-2020 15:28:38

1K+ Views

Use the indexOf() method to search the index of a character in a string.Let’s say the following is our string.String myStr = "amit";Now, let us find the index of character ‘t’ in the above string.strIndex = myStr.indexOf('t');The following is the final example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String myStr = "amit";        int strIndex = 0;        System.out.println("String: "+myStr);        // finding index of character t        strIndex = myStr.indexOf('t');        System.out.println("Character m found at index: "+strIndex);     } }OutputString: amit Character m found at index: 3

Split String with Comma (,) in Java

karthikeya Boyini
Updated on 13-Sep-2023 15:45:12

34K+ Views

Let’s say the following is our string.String str = " This is demo text, and demo line!";To split a string with comma, use the split() method in Java.str.split("[,]", 0);The following is the complete example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "This is demo text, and demo line!";        String[] res = str.split("[,]", 0);        for(String myStr: res) {           System.out.println(myStr);        }     } }OutputThis is demo text and demo line!

Split String with Dot (.) in Java

Samual Sam
Updated on 26-Dec-2024 20:41:49

13K+ Views

In Java, strings are one of the most commonly used data types for storing text. Sometimes, you may need to split a string based on a specific delimiter, such as a dot (.). Java provides powerful string manipulation methods like split(). Split() method  The split() method of Java String is used to divide a string into an array of substrings. This method takes a string in the form of a regular expression as a parameter, searches the currently existing string with the given pattern, and splits it at every occurrence of the pattern matched. Split String with Dot (.) in ... Read More

Advertisements