removeSurrounding

When this char sequence starts with the given prefix and ends with the given suffix, returns a new char sequence having both the given prefix and suffix removed. Otherwise, returns a new char sequence with the same characters.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val textCharSequence: CharSequence = StringBuilder("[content]")
println(textCharSequence.removeSurrounding("[", "]")) // content
// Does not start with prefix
println(StringBuilder("content]").removeSurrounding("[", "]")) // content]
// Does not end with suffix
println(StringBuilder("[content").removeSurrounding("[", "]")) // [content
// Does not start or end with prefix/suffix
println(StringBuilder("content").removeSurrounding("[", "]")) // content
// Empty content
println(StringBuilder("[]").removeSurrounding("[", "]")) // 
// Different delimiters
println(StringBuilder("<content>").removeSurrounding("[", "]")) // <content> 
   //sampleEnd
}

Removes from a string both the given prefix and suffix if and only if it starts with the prefix and ends with the suffix. Otherwise, returns this string unchanged.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val textString = "[content]"
println(textString.removeSurrounding("[", "]")) // content
// Does not start with prefix
println("content]".removeSurrounding("[", "]")) // content]
// Does not end with suffix
println("[content".removeSurrounding("[", "]")) // [content
// Does not start or end with prefix/suffix
println("content".removeSurrounding("[", "]")) // content
// Empty content
println("[]".removeSurrounding("[", "]")) // 
// Different delimiters
println("<content>".removeSurrounding("[", "]")) // <content> 
   //sampleEnd
}

When this char sequence starts with and ends with the given delimiter, returns a new char sequence having this delimiter removed both from the start and end. Otherwise, returns a new char sequence with the same characters.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val textCharSequence: CharSequence = StringBuilder("***content***")
println(textCharSequence.removeSurrounding("***")) // content
// Does not end with delimiter
println(StringBuilder("##content").removeSurrounding("##")) // ##content
// Does not start with delimiter
println(StringBuilder("content##").removeSurrounding("##")) // content##
// No delimiters
println(StringBuilder("content").removeSurrounding("##")) // content
// Empty content
println(StringBuilder("****").removeSurrounding("**")) // 
// Delimiter is single char, removes only one pair
println(StringBuilder("!!!content!!!").removeSurrounding("!")) // !!content!! 
   //sampleEnd
}

Removes the given delimiter string from both the start and the end of this string if and only if it starts with and ends with the delimiter. Otherwise, returns this string unchanged.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val textString = "***content***"
println(textString.removeSurrounding("***")) // content
// Does not end with delimiter
println("##content".removeSurrounding("##")) // ##content
// Does not start with delimiter
println("content##".removeSurrounding("##")) // content##
// No delimiters
println("content".removeSurrounding("##")) // content
// Empty content
println("****".removeSurrounding("**")) // 
// Delimiter is single char, removes only one pair
println("!!!content!!!".removeSurrounding("!")) // !!content!! 
   //sampleEnd
}