- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9316 Articles for Object Oriented Programming
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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!
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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