How to delete spaces at the beginning and end of a string in an Excel Cell

Source: Internet
Author: User


Function: Remove the spaces at the beginning and end of the cell in Excel and retain the spaces in the middle of the string.

Functions used: Mid, find, left, right, trim, substitute, Len

Formula: = Mid (A1, find (left (TRIM (A1), A1), find ("☆", substitute (A1, right (TRIM (A1 )), "☆", Len (A1)-len (substitute (A1, right (TRIM (A1)," ")-find (left (TRIM (A1 )), a1) + 1)

Description: Set the cell to A1. The value is $ AB $ ABA $ ($ indicates space)

1: Find the first non-space characters (RC: Right char) on the Right of A1)
Rc = right (TRIM (A1) ----> rc =

2: Find the number of times RC appears in A1.
N = Len (A1)-len (substitute (A1, RC, "") ----> 3
Substitute (A1, RC, ""): replace RC with null characters

3
: Replace the last RC in A1 with "☆" (because" ☆" is not commonly used) (S: string)
S = substitute (A1, "☆", n) ----> get string $ AB ☆$ $

4
: Find the "☆" position (that is, the first non-space character on the right) (RF: Right first)
Rf = find ("☆", S) ----> 10

5
: Calculate the position of the first non-space character on the left (LF: Left first)
LF = find (left (TRIM (A1), A1) ----> 4

6
: RF-LF + 1 characters starting from LF in A1
= Mid (A1, lf, RF-LF + 1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.