Found 34494 Articles for Programming

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

943 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

30K+ 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-Jun-2020 15:30:03

11K+ Views

Let’s say the following is our string.String str = "This is demo text.This is sample text!";To split a string with dot, 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.This is sample text!";        String[] res = str.split("[.]", 0);        for(String myStr: res) {           System.out.println(myStr);        }     } }OutputThis is demo text This is sample text!

Tokenizing a String in Java

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) {        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!

Difference between equals() vs equalsIgnoreCase() in Java

Samual Sam
Updated on 26-Jun-2020 14:56:37

4K+ Views

Use equals() in Java to check for equality between two strings.Use equalsIgnoreCase() in Java to check for equality between two strings ignoring the case.Let’s say the following are our two strings −String one = "qwerty"; String two = "Qwerty";Both are equal, but the case is different. Since the method ignores case, both of these strings would be considered equal using equalsIgnoreCase() method.Here, we are checking the same −if(one.equalsIgnoreCase(two)) {     System.out.println("String one is equal to two (ignoring the case) i.e. one==two"); }else{     System.out.println("String one is not equal to String two (ignoring the case) i.e. one!=two"); }However, under ... Read More

equals() vs == in Java

Deepti S
Updated on 29-Aug-2023 14:42:50

454 Views

There are two ways to determine if two objects are equal in Javal: the.equals() method and the == operator. The.equals() function compares the contents of two objects. The == operator compares the references of two objects. When you create an object with the new operator, it gets assigned a specified memory location in the heap. Tak, for example, two objects are having the same data. Even if the two objects kept in different sections of memory, the.equals() method will return true. The == operator returns true if two objects get stored in memory at the exact same location. Differences between ... Read More

Display the Operating System name in Java

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: ");        System.out.println(System.getProperty("os.name"));     } }OutputOperating System: Linux

Get the default system properties in Java

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

568 Views

To return all the default systems properties in Java, firstly use the System.getProperties() method.java.util.Properties prop = System.getProperties();After that, use the list() method to list all of them.prop.list(System.out);Example Live Demopublic class Demo {     public static void main(String[] args) {        java.util.Properties prop = System.getProperties();        System.out.println("Here are the Properties:");        prop.list(System.out);     } }OutputHere are the Properties: java.runtime.name=OpenJDK Runtime Environment sun.boot.library.path=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0... java.vm.version=25.141-b16 java.vm.vendor=Oracle Corporation java.vendor.url=http://java.oracle.com/ path.separator=: java.vm.name=OpenJDK 64-Bit Server VM file.encoding.pkg=sun.io user.country=US sun.java.launcher=SUN_STANDARD sun.os.patch.level=unknown java.vm.specification.name=Java Virtual Machine Specification user.dir=/home/cg/root/3757524 java.runtime.version=1.8.0_141-b16 java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment java.endorsed.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0... os.arch=amd64 java.io.tmpdir=/tmp line.separator= java.vm.specification.vendor=Oracle Corporation os.name=Linux sun.jnu.encoding=UTF-8 java.library.path=/home/cg/root/GNUstep/Library/Librari... java.specification.name=Java Platform API Specification java.class.version=52.0 sun.management.compiler=HotSpot 64-Bit Tiered Compilers os.version=3.10.0-862.9.1.el7.x86_64 user.home=? user.timezone= java.awt.printerjob=sun.print.PSPrinterJob ... Read More

Return all system properties in Java

Samual Sam
Updated on 26-Jun-2020 14:43:51

1K+ Views

To return all the system properties in Java, firstly use the System.getProperties() method.java.util.Properties prop = System.getProperties();After that, use the list() method to list all of them.prop.list(System.out);Example Live Demopublic class MainClass {     public static void main(String[] args) {        java.util.Properties prop = System.getProperties();        prop.list(System.out);     } }Outputjava.runtime.name=OpenJDK Runtime Environment sun.boot.library.path=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0... java.vm.version=25.141-b16 java.vm.vendor=Oracle Corporation java.vendor.url=http://java.oracle.com/ path.separator=: java.vm.name=OpenJDK 64-Bit Server VM file.encoding.pkg=sun.io user.country=US sun.java.launcher=SUN_STANDARD sun.os.patch.level=unknown java.vm.specification.name=Java Virtual Machine Specification user.dir=/home/cg/root/3757524 java.runtime.version=1.8.0_141-b16 java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment java.endorsed.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0... os.arch=amd64 java.io.tmpdir=/tmp line.separator= java.vm.specification.vendor=Oracle Corporation os.name=Linux sun.jnu.encoding=UTF-8 java.library.path=/home/cg/root/GNUstep/Library/Librari... java.specification.name=Java Platform API Specification java.class.version=52.0 sun.management.compiler=HotSpot 64-Bit Tiered Compilers os.version=3.10.0-862.9.1.el7.x86_64 user.home=? user.timezone= java.awt.printerjob=sun.print.PSPrinterJob file.encoding=UTF-8 java.specification.version=1.8 user.name=? java.class.path=/home/cg/root/GNUstep/Library/Librari... java.vm.specification.version=1.8 sun.arch.data.model=64 java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0... sun.java.command=MainClass java.specification.vendor=Oracle Corporation user.language=en ... Read More

Advertisements