📜  Java String 类 indent() 方法示例

📅  最后修改于: 2022-05-13 01:54:22.981000             🧑  作者: Mango

Java String 类 indent() 方法示例

JDK 12 在Java.lang.String 类中引入了indent()方法。此方法对于添加或删除开头空格很有用 line 调整每个字符串行的缩进。

句法:

public String indent(int n)

参数:它以整数 n 作为输入并相应地进行缩进。

程序:

当一个字符串被提供给indent()方法时,

  1. 它调用lines()函数
  2. 然后,对于每一行,根据下面讨论的用户案例提供的整数值进行缩进:
    • 如果 n>0(正)
      • 然后在每行的开头添加n个空格,每行都以“\n”为后缀。
    • 如果 n==0
      • 然后缩进保持原样,只有一行以“\n”为后缀。
    • 如果 n<0(负),则
      • 如果 (+n) > 前导空格可用
        • 然后删除每一行的所有前导空格,并以“\n”为后缀
      • 如果 (+n) < 前导空格可用
        • 然后(+n)为每行删除前导空格,每行后缀为“\n”
  3. 然后,在每一行后缀“\n”。
  4. 然后,连接结果字符串行并返回

执行:

示例 1

Java
// Java Program to illustrate 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());
    }
}


Java
// Java Program to illustrate 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
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 illustrate 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