Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

Remove the Last Character from a String in TypeScript

Use str.slice(0, -1) to remove the final UTF-16 code unit from a TypeScript string. For a whole visible Unicode character, use an approach suited to code points or grapheme clusters.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For the common case, use str.slice(0, -1). It returns a new string with the final UTF-16 code unit removed; it does not change the original string. If by “character” you mean a whole visible symbol, choose a Unicode-aware approach instead.

Remove the final UTF-16 code unit

TypeScript uses JavaScript string values, so JavaScript’s slice method works directly:

const shortened = str.slice(0, -1);

The negative end index counts back from the string’s end. The result includes everything before the final code unit and excludes that last unit. Since strings are immutable, assign or return the result rather than expecting str itself to change. If str is empty, the result is also an empty string. TypeScript’s basic types guidance describes strings as primitive values.

Choose the unit you actually want to remove

In everyday language, “character” can mean a UTF-16 code unit, a Unicode code point, or a user-perceived character. Those are not always the same thing.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
What to remove Approach Important distinction
Final UTF-16 code unit str.slice(0, -1) Concise; can remove only half of a surrogate pair.
Final Unicode code point Array.from(str).slice(0, -1).join("") Handles surrogate pairs as one code point, but treats combining marks separately from their base characters.
Final user-perceived character, or grapheme cluster Use grapheme segmentation, such as Intl.Segmenter where supported by the target runtime. Suitable for sequences such as a base letter plus a combining mark or multi-code-point emoji; check support in the runtime you target.

Why a visible symbol can be split

JavaScript string indexing and length are based on UTF-16 code units. Some symbols, including supplementary-plane characters such as 𠮷, are encoded as a pair of code units. Removing one final code unit from a string ending in that symbol can leave half of the surrogate pair, not remove the complete symbol. The TypeScript 1.5 release notes explain this encoding for 𠮷.

Even code-point-aware removal may not match what a person sees as one character: a letter and its combining mark are separate code points, and some emoji are sequences of multiple code points. For text-editing behavior, decide whether your requirement is about code points or grapheme clusters, then test that behavior against the text your application handles.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Make the function’s behavior clear

If the function removes a code unit, name it accordingly:

function removeLastCodeUnit(value: string): string {
  return value.slice(0, -1);
}

Use the primitive TypeScript type string, not the boxed String type; the TypeScript handbook’s basic types guidance recommends primitive types.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.