string 如何使用 Excel 函数获取字符串的最后一个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4362418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:54:33  来源:igfitidea点击:

How do I get the last character of a string using an Excel function?

stringexcelworksheet-function

提问by shlawson

How do I get the last character of a string using an Excel function?

如何使用 Excel 函数获取字符串的最后一个字符?

回答by Phil Hunt

No need to apologize for asking a question! Try using the RIGHTfunction. It returns the last ncharacters of a string.

提出问题无需道歉!尝试使用该RIGHT功能。它返回字符串的最后n 个字符。

=RIGHT(A1, 1)

回答by pnuts

=RIGHT(A1)  

is quite sufficient (where the string is contained in A1).

就足够了(字符串包含在 A1 中)。

Similar in nature to LEFT, Excel's RIGHTfunction extracts a substring from a string starting from the right-most character:

本质上与 LEFT 类似,Excel 的RIGHT函数从字符串中从最右边的字符开始提取子字符串:

SYNTAX

RIGHT( text, [number_of_characters] )

Parameters or Arguments

text

The string that you wish to extract from.

number_of_characters

Optional. It indicates the number of characters that you wish to extract starting from the right-most character. If this parameter is omitted, only 1 character is returned.

Applies To

Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

句法

RIGHT( text, [number_of_characters] )

参数或参数

文本

您希望从中提取的字符串。

字符数

可选的。它表示您希望从最右边的字符开始提取的字符数。如果省略此参数,则仅返回 1 个字符。

适用于

Excel 2016、Excel 2013、Excel 2011 for Mac、Excel 2010、Excel 2007、Excel 2003、Excel XP、Excel 2000

Since number_of_charactersis optional and defaults to 1it is not required in this case.

由于number_of_characters是可选的并且1在这种情况下不需要默认值。

However, there have been many issues with trailing spaces and if this is a risk for the last visiblecharacter (in general):

但是,尾随空格存在很多问题,如果这是最后一个可见字符的风险(通常):

=RIGHT(TRIM(A1))  

might be preferred.

可能是首选。

回答by Dominic Lambert

Looks like the answer above was a little incomplete try the following:-

看起来上面的答案有点不完整,请尝试以下操作:-

=RIGHT(A2,(LEN(A2)-(LEN(A2)-1)))

Obviously, this is for cell A2...

显然,这是针对单元格A2 ...

What this does is uses a combination of Right and Len - Len is the length of a string and in this case, we want to remove all but one from that... clearly, if you wanted the last two characters you'd change the -1 to -2 etc etc etc.

它的作用是使用 Right 和 Len 的组合 - Len 是字符串的长度,在这种情况下,我们希望从中删除除一个以外的所有字符...显然,如果您想要最后两个字符,则需要更改-1 到 -2 等等等等

After the length has been determined and the portion of that which is required - then the Right command will display the information you need.

在确定长度和所需的部分后 - 然后右命令将显示您需要的信息。

This works well combined with an IF statement - I use this to find out if the last character of a string of text is a specific character and remove it if it is. See, the example below for stripping out commas from the end of a text string...

这与 IF 语句结合使用效果很好 - 我使用它来确定文本字符串的最后一个字符是否为特定字符,如果是则将其删除。请参阅下面的示例,用于从文本字符串的末尾去除逗号...

=IF(RIGHT(A2,(LEN(A2)-(LEN(A2)-1)))=",",LEFT(A2,(LEN(A2)-1)),A2)

回答by jrad

Just another way to do this:

只是另一种方法来做到这一点:

=MID(A1, LEN(A1), 1)