Freigeben über


String.StripIndent Method

Definition

Returns a string whose value is this string, with incidental Character#isWhitespace(int) white space removed from the beginning and end of every line.

[Android.Runtime.Register("stripIndent", "()Ljava/lang/String;", "", ApiSince=34)]
public string StripIndent ();
[<Android.Runtime.Register("stripIndent", "()Ljava/lang/String;", "", ApiSince=34)>]
member this.StripIndent : unit -> string

Returns

string with incidental indentation removed and line terminators normalized

Attributes

Remarks

Returns a string whose value is this string, with incidental Character#isWhitespace(int) white space removed from the beginning and end of every line.

Incidental Character#isWhitespace(int) white space is often present in a text block to align the content with the opening delimiter. For example, in the following code, dots represent incidental Character#isWhitespace(int) white space: <blockquote>

String html = """
            ..............&lt;html&gt;
            ..............    &lt;body&gt;
            ..............        &lt;p&gt;Hello, world&lt;/p&gt;
            ..............    &lt;/body&gt;
            ..............&lt;/html&gt;
            ..............""";

</blockquote> This method treats the incidental Character#isWhitespace(int) white space as indentation to be stripped, producing a string that preserves the relative indentation of the content. Using | to visualize the start of each line of the string: <blockquote>

|&lt;html&gt;
            |    &lt;body&gt;
            |        &lt;p&gt;Hello, world&lt;/p&gt;
            |    &lt;/body&gt;
            |&lt;/html&gt;

</blockquote> First, the individual lines of this string are extracted. A line is a sequence of zero or more characters followed by either a line terminator or the end of the string. If the string has at least one line terminator, the last line consists of the characters between the last terminator and the end of the string. Otherwise, if the string has no terminators, the last line is the start of the string to the end of the string, in other words, the entire string. A line does not include the line terminator.

Then, the minimum indentation (min) is determined as follows: <ul> <li>

For each non-blank line (as defined by String#isBlank()), the leading Character#isWhitespace(int) white space characters are counted.

</li> <li>

The leading Character#isWhitespace(int) white space characters on the last line are also counted even if String#isBlank() blank.

</li> </ul>

The min value is the smallest of these counts.

For each String#isBlank() non-blank line, min leading Character#isWhitespace(int) white space characters are removed, and any trailing Character#isWhitespace(int) white space characters are removed. String#isBlank() Blank lines are replaced with the empty string.

Finally, the lines are joined into a new string, using the LF character "\n" (U+000A) to separate lines.

Added in 15.

Java documentation for java.lang.String.stripIndent().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to