Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
Mountain View desk1 min

空白とは?文字列やExcel・Googleスプレッドシートから削除する方法

空白の削除は、端だけを整えるのか、単語間を整えるのか、タブや改行まで消すのかで方法が異なります。Excel、Googleスプレッドシート、Python、JavaScriptでの具体例を紹介します。
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

空白を削除する方法は、何を消したいかで変わります。先頭と末尾だけを整えるなら「トリム」、単語間の連続スペースを1つにするなら表計算のTRIM、タブや改行を含めて空白文字をすべて消すなら、対象文字を指定した置換を使います。見た目が空いていても、通常のスペースではなく改行やノーブレークスペースの場合があるため、目的と文字の種類を確認してから処理しましょう。

空白とは何か、削除前に何を決めるべきか

空白(whitespace)は、通常のスペースだけでなく、タブや改行など、文字列の中で空きを作る文字を指すことがあります。データの出どころによっては、画面上で普通のスペースに見えるノーブレークスペースなどが含まれていることもあります。

処理したい結果を、次のどれかに分けて考えると適切な方法を選びやすくなります。

  • 端だけを取り除く:文字列の先頭と末尾にある空白を消し、内部の区切りは残す。
  • 間隔を整える:先頭・末尾の余分なスペースを除き、単語間に続く通常のスペースを1つにする。
  • 該当する文字をすべて消す:内部のスペースや、指定した場合はタブ・改行も削除する。単語同士がつながることがある。

「トリム」は通常、端の空白を取り除く処理です。内部の空白まで削除したい場合は、別の操作が必要です。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Microsoft Excel Laminated Two-Sided Keyboard Shortcut Guide - Windows Edition
  • Over 215 Microsoft Windows Excel Shortcuts
  • Two-Sided Durable Laminiated Sheet
  • Designed for Excel on a Windows Computer

Excelで余分なスペースを整える

通常のスペースならTRIM関数

セルA1の先頭と末尾にある通常のスペースを取り除き、単語間に複数ある通常のスペースを1つにするには、次を使います。

=TRIM(A1)

この関数は単語間の区切りを残すため、文字をすべて連結する操作ではありません。ExcelのTRIMはASCIIの通常のスペース(値32)を対象として設計されており、ノーブレークスペース(10進数160)は単独では取り除きません。Microsoft SupportのTRIM関数の説明も、「By itself, the TRIM function does not remove this nonbreaking space character.」と案内しています。

インポートしたデータに別の空白文字がある場合

TRIMしても見た目が変わらない場合は、データにノーブレークスペースなど別の文字が含まれている可能性があります。Microsoftは、対象に応じてTRIM、CLEAN、SUBSTITUTEを組み合わせる方法を紹介しています。まず実際の文字を確認し、必要な文字だけをSUBSTITUTEで通常のスペースに置き換えるなどしてから整形してください。CLEANやSUBSTITUTEを無差別に適用すると、必要な文字まで変更するおそれがあります。詳しくはMicrosoftのデータを整理する方法を参照してください。

Rank #2
Sale
Microsoft Surface Pro Keyboard with Pen Storage, Compatible with Copilot+ (11th Edition), Surface 9 and 8, Alcantara Material, Black
  • Instant Copilot. Unlock new possibilities with the dedicated Copilot key, which gives you instant access to experiences that can enhance your productivity¹.
  • Enhance your experience With the new microphone mute key and snipping key
  • Full keyboard experience. Features a full mechanical keyset, backlit keys, and a large trackpad for precise navigation and control. Optimal key spacing allows fast, fluid typing.
  • Slim and compact Performs like a traditional, full-size keyboard.
  • Clicks in place instantly Use in combination with the Surface Pro (11th Edition), Pro 9 and Pro 8* kickstand for a perfect laptop experience anywhere.

Googleスプレッドシートでスペースを整える

セル内の間隔を直す

セルA1に対して、次の数式を使います。

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

=TRIM(A1)

Google SheetsのTRIMは、先頭と末尾の通常のスペースを取り除き、内部で連続する通常のスペースを1つにします。単語間の区切りは維持しますが、ノーブレークスペースはトリムしません。仕様はGoogleのTRIM関数ヘルプで確認できます。

範囲内のセルをまとめて処理する

Google Sheets APIでは、範囲に対するTrimWhitespaceRequestを使い、セル文字列の先頭・末尾の空白を除去し、内部の空白の連続を1つにできます。APIを使う場合は、Sheets APIのRequestsリファレンスに記載されたリクエスト仕様を確認してください。

Rank #3
Pixiecube Excel Cheat Sheet Desk Pad | Excel Shortcut Keys Mouse Pad | Extended Large XL Gaming Mousepad | PC Office Spreadsheet Keyboard Mat | Non-Slip Stitched Edge
  • EXCEL SHORTCUTS. ZERO SEARCHING. – Our bestselling reference mat puts an extensive collection of commonly used commands, formulas and helpful tricks directly beneath your fingertips so you can find answers fast, work smarter and stay in the flow.
  • YOUR DESK. SMARTER. – Clearly organized sections for navigation, selection, formatting, data and functions make it easy to find the right Excel command exactly when you need it.
  • LEARN, WORK & RESET – Built-in desk-exercise diagrams give you 10 quick ways to stretch, recharge and return to work feeling sharper.
  • ROOM TO WORK & CREATE – The extended 31.5 x 11.8-inch Pixiecube desk mat fits a laptop or keyboard and mouse, while the soft 2 mm surface adds comfort and protects your desktop.
  • BUILT FOR REAL-WORLD WORKDAYS – A rugged stitched edge helps prevent fraying, and the water-resistant, stain-resistant surface protects against scratches, spills and everyday wear—because smarter desks should work harder.

シート上のメニューから空白を処理する場合は、対象範囲を選択し、「テキストを分割、重複を削除、空白をトリム」の案内に従います。目に見えない文字の種類によっては、通常のトリムでは残ることがあります。

CLEAN関数で消える文字の範囲

Google SheetsのCLEANは、非印字ASCII文字を対象にします。ASCII以外の非印字Unicode文字を取り除く関数ではありません。ノーブレークスペースなどが残る場合の制限は、GoogleのCLEAN関数ヘルプを参照してください。

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.

Pythonで文字列の空白を処理する

先頭・末尾だけを取り除く

Pythonでは、次の各メソッドが文字列の端を処理します。

Rank #4
Sale
Incase Wired Keyboard 600 – Designed by Microsoft – Spill Resistant, Quiet Touch Keys, Plug and Play, 4 Hotkeys, Windows Start Key – Black
  • Efficient Media Controls: The Wired Keyboard 600, designed by Microsoft, features a Media Center with four hot keys for easy control of play/pause, volume up, volume down, and mute functions.
  • Quiet and Responsive Keys: Enjoy a comfortable typing experience with quiet, thin-profile keys that are both responsive and efficient.
  • Convenient Shortcuts: Quickly access common tasks with dedicated shortcut keys, including a calculator hot key and a Windows start screen key.
  • Spill-Resistant Design: Work confidently with a spill-resistant design that protects your keyboard from accidental messes.
  • Plug-and-Play Simplicity: No software needed—just connect the keyboard to your PC and start using it right away, with a full number pad for efficient data entry.
  • text.strip():両端
  • text.lstrip():先頭側
  • text.rstrip():末尾側

これらは内部の空白を削除しません。Pythonの組み込み型の説明では、stripの既定動作が示されています。

正規表現で空白文字をすべて取り除く

Unicode文字列について、Pythonの正規表現で一致する空白をすべて削除する例は次のとおりです。

re.sub(r"s+", "", text)

Unicodeモードのsは、str.isspace()が空白と判定する文字に一致します。したがって、通常のスペースだけでなく、タブや改行なども対象になり得ます。削除すると単語やデータ項目が連結されるため、それが意図した結果か確認してください。ASCIIモードではsの対象が[ tnrfv]に限られます。詳細はPythonの正規表現操作の説明を参照してください。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
SYNERLOGIC Microsoft Word/Excel (for Windows) Reference Guide Keyboard Shortcut Sticker, Laminated, No-Residue Vinyl (White/Small)
  • 💻 ✔️ EVERY ESSENTIAL SHORTCUT - With the SYNERLOGIC Reference Keyboard Shortcut Sticker, you have the most important shortcuts conveniently placed right in front of you. Easily learn new shortcuts and always be able to quickly lookup commands without the need to “Google” it.
  • 💻✔️ Work FASTER and SMARTER - Quick tips at your fingertips! This tool makes it easy to learn how to use your computer much faster and makes your workflow increase exponentially. It’s perfect for any age or skill level, students or seniors, at home, or in the office.
  • 💻 ✔️ New adhesive – stronger hold. It may leave a light residue when removed, but this wipes off easily with a soft cloth and warm, soapy water. Fewer air bubbles – for the smoothest finish, don’t peel off the entire backing at once. Instead, fold back a small section, line it up, and press gradually as you peel more. The “peel-and-stick-all-at-once” method only works for thin decals, not for stickers like ours.
  • 💻 ✔️ Compatible and fits any brand laptop or desktop running Windows 10 or 11 Operating System.
  • 💻 ✔️ Original Design and Production by Synerlogic Electronics, San Diego, CA, Boca Raton, FL and Bay City, MI, United States 2020. All rights reserved, any commercial reproduction without permission is punishable by all applicable laws.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

JavaScriptで文字列の空白を処理する

端の空白だけを取り除く

JavaScriptでは、text.trim()が両端、text.trimStart()が先頭、text.trimEnd()が末尾の空白を取り除きます。いずれも内部の空白を消すメソッドではありません。仕様はMDNのString.prototype.trim()の説明を確認してください。

内部も削除したい場合

内部の空白も消すには、置換処理で対象文字を指定します。どの文字を空白として扱うかはパターンによって異なるため、タブ、改行、Unicode空白などを含める必要があるかを先に決めてください。trim()だけでは内部の文字は削除されません。

目的に合う方法を選ぶ

目的 方法 内部の区切り 注意点
文字列の端だけを整える Pythonのstrip()、JavaScriptのtrim() そのまま残る 内部の空白は削除しない。
表計算セルの間隔を整える ExcelまたはGoogle SheetsのTRIM 連続する通常のスペースを1つ残す 両者ともノーブレークスペースをそのままではトリムしない。
PythonでUnicode空白をすべて削除する re.sub(r"s+", "", text) 一致した空白を削除する タブや改行も対象になり得る。

処理後に確認すること

  • 単語間のスペースを残すべきか、文字をつなげてよいか。
  • タブや改行を削除すると、表形式や複数行の構造が壊れないか。
  • 見た目に反して残る文字がある場合、ノーブレークスペースなど別種の文字が含まれていないか。
  • 表計算でCLEANを使う場合、その対象がASCIIの非印字文字に限られることを踏まえているか。

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 *

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.