JDK 12在Java.lang.String類中引入了indent()方法。此方法對於從行的開頭添加或刪除空格以調整每個字符串行的縮進很有用。
用法:
public String indent(int n)
參數:它以整數n作為輸入並相應地進行縮進。
Also, each line is suffixed with “\n” (a newline character).
程序:
當將字符串提供給indent()方法時,
- 它調用lines()函數
- 然後,針對每行,根據以下討論的用戶情況提供的整數值進行縮進:
- 如果n> 0(正)
- 然後,在每行的開頭添加n個空格,並在每行後綴“\n”。
- 如果n == 0
- 然後縮進保持不變,隻有行後綴“\n”。
- 如果n <0(負),則
- 如果(+ n)>前導空格可用
- 然後,刪除每行的所有前導空格,並為每行添加“\n”後綴
- 如果(+ n)<前導空白可用
- 然後,為每行刪除(+ n)前導空格,並為每行添加後綴“\n”
- 如果(+ n)>前導空格可用
- 如果n> 0(正)
- 然後,在每行後綴“\n”。
- 然後,連接結果字符串行並返回
實現方式:
例子1
Java
// Java Program to illustarte indent() method of
// String class
// Importing basic libraries
import java.io.*;
import java.util.*;
// Class for indent() method
public class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input string
String input
= "GeeksforGeeks\nA Computer Science portal for geeks.";
// Print and display the input string
System.out.println(input);
// Print the above string length
// using standard length() method
System.out.println("Input String length:"
+ input.length());
// Now, calling the indent() method
// for random value of N
// Case 1:N>0 | Positive
// Say N = 5 which is positive
// so as per procedural algorithm
// 5 white spaces are added
// at the starting of each line
String output = input.indent(5);
// Print and display output string
System.out.println(output);
// Print the new string length
// again using the length() method
System.out.println("New String length:"
+ output.length());
// Case 2:N=0 | Zero
// Call indent method with n=0
String output1 = input.indent(0);
System.out.println(output1);
System.out.println("New String length:"
+ output1.length());
// Case 3:N < 0 | Negative
// Call indent method with n=-3 (negative)
String output2 = input.indent(-3);
// Print the output string
System.out.println(output);
// Print output(new) string length
System.out.println("New String length:"
+ output2.length());
}
}
輸出:
GeeksforGeeks A Computer Science portal for geeks. Input String length:50 GeeksforGeeks A Computer Science portal for geeks. New String length:61 GeeksforGeeks A Computer Science portal for geeks. New String length:51 GeeksforGeeks A Computer Science portal for geeks. New String length:51
範例2:
Java
// Java Program to illustarte indent() method of
// String class
// Importing basic libraries
import java.util.*;
import java.io.*;
// Class for indent() method
public class GFG {
// Main driver method
public static void main(String args[])
{
// Input string
String input = "GeeksforGeeks";
System.out.println(input);
System.out.println("Input String length:"
+ input.length());
// Call indent method on input string with n=5
// (positive)
String output = input.indent(5);
System.out.println(output);
System.out.println("New String length:"
+ output.length());
// Call indent method on output string with n=0
String output1 = output.indent(0);
System.out.println(output1);
System.out.println("New String length:"
+ output1.length());
// Call indent method on output1 string with n=-3
// (negative)
String output2 = output.indent(-3);
System.out.println(output2);
System.out.println("New String length:"
+ output2.length());
}
}
輸出:
GeeksforGeeks Input String length:13 GeeksforGeeks New String length:19 GeeksforGeeks New String length:19 GeeksforGeeks New String length:16
相關用法
- CSS text-indent用法及代碼示例
- Java Class forName(String, boolean, ClassLoader)用法及代碼示例
- Java String repeat()用法及代碼示例
- Java String stripTrailing()用法及代碼示例
- Java String strip()用法及代碼示例
- Java String stripLeading()用法及代碼示例
- Java String lines()用法及代碼示例
- Java Java.util.concurrent.RecursiveAction用法及代碼示例
- Java Java.util.concurrent.RecursiveTask用法及代碼示例
- Java Java.awt.image.RescaleOp用法及代碼示例
- Java Java.util.concurrent.Phaser用法及代碼示例
- Java Class getModule()用法及代碼示例
- Java Class getPackage()用法及代碼示例
- Java Class getPackageName()用法及代碼示例
- Java Class getTypeParameters()用法及代碼示例
注:本文由純淨天空篩選整理自hemavatisabu大神的英文原創作品 Java String Class indent() Method With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。