![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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
How to count the number characters in a Java string?
Declare an integer, initialize it with 0, in for loop increment it for each character.
Example
public class Sample { public static void main(String args[]) { String str = new String("Hi welcome to Tutorialspoint"); int count = 0; for(int i = 0; i<str.length(); i++) { count++; } System.out.println("Number characters in the given string (including spaces) "+count); } }
Output
Number characters in the given string (including spaces) 28
Advertisements