HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 12 / String indent(count) – Left indent lines in Java

String indent(count) – Left indent lines in Java

Learn to indent (left indentation) a string(s) in Java using String.indent() API. This api has been introduced in Java 12.

1. String.indent(count) API

This method adjusts the indentation of each line of a given string based on the value of count, and normalizes line termination characters.

/**
* count - number of leading white space characters to add or remove
* returns - string with indentation adjusted and line endings normalized
*/
public String indent​(int count)

Note that the value of count can be either positive or nagative number.

  • positive number – If count > 0 then spaces are inserted at the beginning of each line.
  • negative number – If count < 0 then spaces are removed at the beginning of each line.
  • negative number – If count > available white spaces then all leading spaces are removed.

Each white space character is treated as a single character. In particular, the tab character “\t” is considered a single character; it is not expanded.

2. String.indent​() Example

Java program to white strings into file indented to 8 characters.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.stream.Stream;

public class Main 
{
	public static void main(String[] args) 
	{
		try 
		{
			Path file = Files.createTempFile("testOne", ".txt");

			//Write strings to file indented to 8 leading spaces
			Files.writeString(file, "ABC".indent(8), StandardOpenOption.APPEND);
			Files.writeString(file, "123".indent(8), StandardOpenOption.APPEND);
			Files.writeString(file, "XYZ".indent(8), StandardOpenOption.APPEND);	

			//Verify the content
			Stream<String> lines = Files.lines(file);

			lines.forEach(System.out::println);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}

Program output.

        ABC
        123
        XYZ

Drop me your questions related to reading a file into lines of stream.

Happy Learning !!

Ref : Java doc

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java 12 Tutorial

  • Java 12 – New Features
  • Java 12 – Teeing Collector
  • Java 12 – String.indent()
  • Java 12 – Compact Number Format

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces