- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to extract the first n characters from a string using Java?
To find the consonants in the given String compare every character in it using the charAt() method with the vowel letters and remaining are consonants.
Example
public class FindingConsonants { public static void main(String args[]) { String str = new String("Hi Welcome to Tutorialspoint"); for(int i=0; i<str.length(); i++) { if(str.charAt(i) == 'a'|| str.charAt(i) == 'e'|| str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u'|| str.charAt(i) == ' ') { }else{ System.out.println("Given string contains "+str.charAt(i)+" at the index "+i); } } } }
Output
Given string contains H at the index 0 Given string contains W at the index 3 Given string contains l at the index 5 Given string contains c at the index 6 Given string contains m at the index 8 Given string contains t at the index 11 Given string contains T at the index 14 Given string contains t at the index 16 Given string contains r at the index 18 Given string contains l at the index 21 Given string contains s at the index 22 Given string contains p at the index 23 Given string contains n at the index 26 Given string contains t at the index 27
- Related Articles
- How to extract the last n characters from a string using Java?
- How to extract first two characters from a string in R?
- JavaScript - Remove first n characters from string
- How to extract characters from a string in R?
- How to extract certain substring from a string using Java?
- How to get first N characters from a MySQL column?
- How to extract an HTML tag from a String using regex in Java?
- How to extract initial, last, or middle characters from a string in R?
- How to extract numbers from a string using Python?
- Extract only characters from given string in Python
- How to extract each (English) word from a string using regular expression in Java?
- How to extract multiple integers from a String in Java?
- How to extract numbers from a string using regular expressions?
- Python program to extract characters in given range from a string list
- How to extract first n values from each element in an R list?

Advertisements