Found 9316 Articles for Object Oriented Programming

Java Program to locate a character in a string

Samual Sam
Updated on 21-Jun-2024 11:27:10

13K+ Views

To locate a character in a string, use the indexOf() method.Let’s say the following is our string.String str = "testdemo";Find a character ‘d’ in a string and get the index.int index = str.indexOf( 'd');Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "testdemo";       System.out.println("String: "+str);       int index = str.indexOf( 'd' );       System.out.printf("'d' is at index %d, index);    } }OutputString: testdemo 'd' is at index 4Let us see another example. The method returns -1, if the character isn’t found −Example Live Demopublic class ... Read More

Check that the String does not contain certain characters in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:09:03

3K+ Views

Let’s say the following is our string with special characters.String str = "test*$demo";Check for the special characters.Pattern pattern = Pattern.compile("[^A-Za-z0-9]"); Matcher match = pattern.matcher(str); boolean val = match.find();Now, if the bool value “val” is true, that would mean the special characters are in the string.if (val == true) System.out.println("Special characters are in the string.");Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String []args) {       String str = "test*$demo";       System.out.println("String: "+str);       Pattern pattern = Pattern.compile("[^A-Za-z0-9]");       Matcher match = pattern.matcher(str);       boolean val ... Read More

Check whether the String contains only digit characters in Java

Samual Sam
Updated on 27-Jun-2020 06:10:48

954 Views

The following is our string.String str = "4434";To check whether the above string has only digit characters, try the following if condition that uses matches() method and checks for every character.if(str.matches("[0-9]+") && str.length() > 2) { System.out.println("String contains only digits!"); }Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "4434";       if(str.matches("[0-9]+") && str.length() > 2) {          System.out.println("String contains only digits!");       }    } }OutputString contains only digits!Let us see another example.Example Live Demopublic class Demo {    public static void main(String []args) ... Read More

Delete all whitespaces from a String in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:01:21

103 Views

To delete all whitespaces from a string, use the replaceAll() method and replace every whitespace with empty.Let’s say the following is our string.String str = "This is it!";Now, let us replace the whitespaces that will eventually delete them.String res = str.replaceAll("\s+","");Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "This is it!";       System.out.println("String: "+str);       String res = str.replaceAll("\s+","");       System.out.println("String after deleting whitespace: "+res);    } }OutputString: This is it! String after deleting whitespace: Thisisit!

Java Program to swap the case of a String

Samual Sam
Updated on 18-Jun-2024 18:01:46

12K+ Views

To swap the case of a string, use.toLowerCase() for title case string. toLowerCase() for uppercase stringtoUpperCase() for lowercase stringLoop through what we discussed above for all the characters in the sting.for (int i = 0; i > len; i++) {    c = str.charAt(i);    // title case converted to lower case    if (Character.isTitleCase(c)) {       c = Character.toLowerCase(c);    }    // upper case converted to lower case    if (Character.isUpperCase(c)) {       c = Character.toLowerCase(c);    }    // lower case converted to upper case    if (Character.isLowerCase(c)) {       c ... Read More

Java Program to construct one String from another

karthikeya Boyini
Updated on 27-Jun-2020 05:44:55

414 Views

To construct on string from another, firstly take a charcter array for the first string.char ch[] = { 'A', 'M', 'I', 'T' }; String str1 = new String(ch);The above forms first string. Now, let us created another string from the first string.String str2 = new String(str1);In this way, we can easily construct one string from another.Example Live Demopublic class Demo {    public static void main(String[] args) {       char ch[] = { 'A', 'M', 'I', 'T' };       String str1 = new String(ch);       String str2 = new String(str1);       String str3 ... Read More

Extracting a substring as an array of characters in Java

karthikeya Boyini
Updated on 27-Jun-2020 05:47:28

3K+ Views

To extract a substring as an array of characters in Java, use the getChars() method.Let’s say the following is our string and character array.String str = "World is not enough!"; char[] chArr = new char[10];Now, use the getChars() method to extract a substring.str.getChars(13, 19, chArr, 0);The above substring is an array of characters which can be displayed as shown in the complete example below −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "World is not enough!";       char[] chArr = new char[10];       str.getChars(13, 19, chArr, ... Read More

Creating a string from a subset of the array elements in Java

Samual Sam
Updated on 27-Jun-2020 05:48:09

350 Views

To get a string from a subset of the character array elements, use the copyValueOf() method. This method returns a String that represents the character sequence in the array specified.Here is our character array.char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};Now, let us create a string from the subset of the above array elements.String str = String.copyValueOf(ch, 4, 2);Example Live Demopublic class Demo {    public static void main(String[] args) {       char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};       String str = String.copyValueOf(ch, 4, 2);       System.out.println(str);    } }OutputIN

Creating String Object from certain part of a character Array in Java

karthikeya Boyini
Updated on 27-Jun-2020 05:49:54

79 Views

Here is our character array.char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};Create string object from some part of a string using the following String constructor. Through this we are fetching substring “IN” from the character array.String str = new String(ch, 4, 2);Example Live Demopublic class Demo {    public static void main(String[] args) {       char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};       String str = new String(ch, 4, 2);       System.out.println(str);    } }OutputIN

Creating String Object from Character Array in Java

Samual Sam
Updated on 27-Jun-2020 05:50:37

469 Views

Here is our character array.char[] ch = { 'T', 'E', 'S', 'T'};To create string object from the above character array is quite easy. Add the array to the string parameter as shown below −String str = new String(ch);Example Live Demopublic class Demo {    public static void main(String[] args) {       char[] ch = { 'T', 'E', 'S', 'T'};       String str = new String(ch);       System.out.println(str);    } }OutputTEST

Advertisements