String.StripIndent Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns this string with incidental 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 this string with incidental white space removed from the beginning and end of every line.
Incidental white space recognized by Character.IsWhitespace(int) is often present in a text block to align the content with the opening delimiter. For example, in the following code, dots represent incidental white space:
String html = """
..............<html>
.............. <body>
.............. <p>Hello, world</p>
.............. </body>
..............</html>
..............""";
This method treats incidental white space recognized by Character.IsWhitespace(int) 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:
|<html>
| <body>
| <p>Hello, world</p>
| </body>
|</html>
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:
- For each non-blank line (a line whose
IsBlankproperty isfalse), the leading characters recognized as white space byCharacter.IsWhitespace(int)are counted.: - The leading characters recognized as white space by
Character.IsWhitespace(int)on the last line are also counted, even if the last line'sIsBlankproperty istrue.:
The min value is the smallest of these counts.
For each non-blank line, min leading characters recognized as white space by Character.IsWhitespace(int) are removed, along with any trailing characters recognized as white space. Lines whose IsBlank property is true 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.