Wolfram: Delete SubString

By Xah Lee. Date: . Last updated: .

Delete Substring, by Index

StringDrop
  • StringDrop[ str, n] → delete first n chars.
  • StringDrop[ str, -n] → delete last n chars.
  • StringDrop[ str, {n}] → delete nth char.
  • StringDrop[ str, {n, m}] → delete nth to mth.
  • StringDrop[ strList, spec ] → work on multiple strings.

StringDrop

(* drop first 3 *)
StringDrop[ "abcdefg", 3 ]
(* defg *)

(* last 3 *)
StringDrop[ "abcdefg", -3 ]
(* abcd *)

(* drop 1 char *)
StringDrop[ "abcdefg", {3} ]
(* abdefg *)

(* 3 to 5 *)
StringDrop[ "abcdefg", {3,5} ]
(* abfg *)

(* do on multiple strings *)
StringDrop[ {"abc", "def"}, 2 ]
(* {c, f} *)

Trim String

StringTrim
  • StringTrim[ str ] → delete whitespace at beginning and end.
  • StringTrim[ str, pattern ] → delete pattern at beginning and end.

StringTrim

(* trim whitespace *)
StringTrim[ " abc\n \n" ]
(* abc *)

(* trim x *)
StringTrim[ "xAxx", "x" ]
(* Ax *)

(* trim one or more x *)
StringTrim[ "xAxx", RegularExpression["x+"] ]
(* A *)

StringTrim[ "XAXX", RegularExpression["x+"] , IgnoreCase -> True ]
(* A *)

Delete Substrings by Pattern

StringDelete

Delete parts by Regular Expression .

StringDelete

StringDelete[ "some thing", "thing" ]
(* some  *)

StringDelete[ "some thing", RegularExpression[ "[aeiou]" ] ]
(* sm thng *)

WolframLang String