- 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
Check if a string contains a number using Java.
To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class.
Example
public class ContainsExample { public static void main(String args[]){ String sample = "krishna64"; char[] chars = sample.toCharArray(); StringBuilder sb = new StringBuilder(); for(char c : chars){ if(Character.isDigit(c)){ sb.append(c); } } System.out.println(sb); } }
Output
64
Advertisements