- 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
Displaying at most 10 characters in a string in Java
To display at most 10 characters in a string, work with the Formatter class.
Import the following package for Formatter class in Java −
import java.util.Formatter;
Create a new Formatter object −
Formatter f = new Formatter();
Let us now display at most 10 characters in a string −
f = new Formatter(); System.out.println("Displaying at most 10 characters: "+f.format("%.10s", "This is demo text!"));
The following is an example −
Example
import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); String str = "This is demo text!"; System.out.println("String: "+str); // displaying at most 10 characters f = new Formatter(); System.out.println("Displaying at most 10 characters: "+f.format("%.10s", "This is demo text!")); } }
Output
String: This is demo text! Displaying at most 10 characters: This is de
- Related Articles
- Searching characters in a String in Java.
- Longest Substring with At Most Two Distinct Characters in C++
- Longest Substring with At Most K Distinct Characters in C++
- Swapping Characters of a String in Java
- Searching characters and substring in a String in Java
- Displaying content of a HashMap in Java
- How to find the first 10 characters of a string in C#?
- Java Program to find duplicate characters in a String?
- Set characters at a specific position within the string in Arduino
- Convert a String to a List of Characters in Java
- Displaying Department Name Having Most Number of Employees in SQL Server
- Delete the first 10 characters from JTextArea in Java
- Java program to find all duplicate characters in a string
- Remove all non-alphabetical characters of a String in Java?
- How to count the number characters in a Java string?

Advertisements