Found 9326 Articles for Object Oriented Programming

How to convert a Java String to an Int?

Alankritha Ammu
Updated on 30-Jul-2019 22:30:21

226 Views

The parseXxx() method is used to get the primitive data type of a certain String. In this case to convert a String to integer you could use the parseInt() method.Example Live Demopublic class Test {    public static void main(String args[]) {       int x =Integer.parseInt("9");       double c = Double.parseDouble("5");       int b = Integer.parseInt("444",16);       System.out.println(x);       System.out.println(c);       System.out.println(b);    } }Output9 5.0 1092

Checking for Null or Empty or White Space Only String in Java.

Anjana
Updated on 30-Jul-2019 22:30:21

2K+ Views

We can verify whether the given string is empty using the isEmpty() method of the String class. This method returns true only if length() is 0.Example Live Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str = "tutorialspoint";       // prints length of string       System.out.println("length of string = " + str.length());       // checks if the string is empty or not       System.out.println("is this string empty? = " + str.isEmpty());    } }Outputlength of string = 14 is this string empty? = false

Replace String with another in java.

Anjana
Updated on 30-Jul-2019 22:30:21

331 Views

The replace() method of the returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.Example Live Demoimport java.io.*; public class Test {    public static void main(String args[]) {       String Str = new String("Welcome to Tutorialspoint.com");       System.out.print("Return Value :" );       System.out.println(Str.replace('o', 'T'));       System.out.print("Return Value :" );       System.out.println(Str.replace('l', 'D'));    } }OutputReturn Value :WelcTme tT TutTrialspTint.cTm Return Value :WeDcome to TutoriaDspoint.com

How to remove a particular character from a String.

Manikanth Mani
Updated on 18-Jun-2020 08:01:46

206 Views

Following example shows how to remove a character from a particular position from a string with the help of removeCharAt(string, position) method.ExampleLive Demopublic class Sample {    public static void main(String args[]) {       String str = "this is Java";       System.out.println(removeCharAt(str, 3));    }    public static String removeCharAt(String s, int pos) {       return s.substring(0, pos) + s.substring(pos + 1);    } }Outputthis is Java

How to split a Java String into tokens with StringTokenizer?

Abhinanda Shri
Updated on 26-Feb-2020 07:06:25

373 Views

The hasMoreTokens() method is used to test if there are more tokens available from this tokenizer's string.ExampleLive Demoimport java.util.*; public class StringTokenizerDemo {    public static void main(String[] args) {       // creating string tokenizer       StringTokenizer st = new StringTokenizer("Come to learn");       // checking elements       while (st.hasMoreElements()) {          System.out.println("Next element : " + st.nextElement());       }    } }OutputNext element : Come Next element : to Next element : learn

String in Switch Case in Java

Govinda Sai
Updated on 30-Jul-2019 22:30:21

143 Views

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.Example Live Demopublic class Test {    public static void main(String args[]) {       // char grade = args[0].charAt(0);       char grade = 'C';       switch(grade) {          case 'A' :             System.out.println("Excellent!");             break;          case 'B' :          case 'C' :   ... Read More

Java string comparison sample code examples

Ramu Prasad
Updated on 30-Jul-2019 22:30:21

81 Views

We can compare Strings in Java using the compareTo() method and the == operator.comapareTo() method: The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.The == operator: You can compare two strings using == operator. But, it compares references to the given variables, not values.Example Live Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str1 = "tutorials", str2 = "point";     ... Read More

String compare by == operator in Java

V Jyothi
Updated on 26-Feb-2020 06:23:02

84 Views

You can compare two strings using == operator. But, it compares references of the given variables, not values.ExampleLive Demopublic class Sample {    public static void main(String args[]){       String str1 = "hello";       String str2 = "hello";       System.out.println(str1 == str2);    } }Outputtrue

What is the append method in Java?

Moumita
Updated on 30-Jul-2019 22:30:21

3K+ Views

The append(char c) method of the java.lang.StringBuffer appends the string representation of the char argument to this sequence. The argument is appended to the contents of this sequence. The length of this sequence increases by 1.Example Live Demoimport java.lang.*; public class StringBufferDemo {    public static void main(String[] args) {       StringBuffer buff = new StringBuffer("tuts ");       System.out.println("buffer = " + buff);             // appends the char argument as string to the string buffer.       buff.append('A');             // print the string buffer after appending ... Read More

String compare by equals() method in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:21

92 Views

The equals() method of the String class compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Example Live Demopublic class Test {    public static void main(String args[]) {       Integer x = 5;       Integer y = 10;       Integer z =5;       Short a = 5;       System.out.println(x.equals(y));       System.out.println(x.equals(z));       System.out.println(x.equals(a));    } }Outputfalse true false

Advertisements