文本函数

本节介绍「文本」函数。

要访问此命令...

插入 - 函数 - 类别「文字


Using double quotation marks in formulas

To include a text string in a formula, place the text string between two double quotation marks (") and Calc takes the characters in the string without attempting to interpret them. For example, the formula ="Hello world!" displays the text string Hello world! in the cell, with no surrounding double quotation marks.

The more complex formula =CONCATENATE("Life is really simple, "; "but we insist on making it complicated "; "(Confucius).") concatenates three individual strings in double quotation marks, outputting Life is really simple, but we insist on making it complicated (Confucius).

To place a literal double quotation mark within a string inside a formula, two methods can be used:

  1. You can "escape" the double quotation mark with an additional double quotation mark, and Calc treats the escaped double quotation mark as a literal value. For example, the formula ="My name is ""John Doe""." outputs the string My name is "John Doe". Another simple example is the formula =UNICODE("""") which returns 34, the decimal value of the Unicode quotation mark character (U+0022) — here the first and fourth double quotation marks indicate the beginning and end of the string, while the second double quotation mark escapes the third.

  2. You can use the CHAR function or the UNICHAR function to insert a double quotation mark. For example, the formula =UNICHAR(34) & "The Catcher in the Rye" & UNICHAR(34) & " is a famous book by J. D. Salinger." displays the string "The Catcher in the Rye" is a famous book by J. D. Salinger.

Be aware that Calc's AutoCorrect function may modify double quotation marks. AutoCorrect should not change the double quotation marks within formula cells but may change those used in non-formula cells containing text. For example, if you copy a string that is surrounded by some other form of typographical double quotation marks, such as the left double quotation mark (U+201C) and the right double quotation mark (U+201D), and then paste into a formula cell, an error may result. Open the Double Quotes area of the Tools - AutoCorrect Options - Localized Options dialog to set the characters used to automatically correct the start and end typographical double quotation marks. Uncheck the Replace toggle button to disable the feature.

ARABIC

Returns the numeric value corresponding to a Roman number expressed as text.

ASC

Converts double-byte (full-width) characters to single-byte (half-width) ASCII and katakana characters.

JIS

Converts single-byte (half-width) ASCII or katakana characters to double-byte (full-width) characters.

REGEX

使用正则表达式匹配、提取或选择性地替换文本。

ROMAN

Converts a number into a Roman numeral. The value range must be between 0 and 3999. A simplification mode can be specified in the range from 0 to 4.

VALUE

Converts the string representation of a number to numeric form. If the supplied string is a valid date, time, or date-time, the corresponding date-time serial number is returned.

WEBSERVICE

从 URI 获取一些 web 内容。

FILTERXML

对 XML 文档应用 XPath 表达式。

ENCODEURL

返回 URL 编码的字符串。

BAHTTEXT

将数字转换为泰语文本,包括泰国货币名称。

语法

BAHTTEXT(数字)

数字」为任意数字。"Baht" 将被附加到该数字的整数部分,而 "Satang" 将被附加到该数字的小数部分。

示例

=BAHTTEXT(12.65)」返回一个以泰语字符表示的字符串,该字符串的含义为「12 铢和 65 萨当」。

Technical information

This function is not part of the Open Document Format for Office Applications (OpenDocument) Version 1.3. Part 4: Recalculated Formula (OpenFormula) Format standard. The name space is

COM.MICROSOFT.BAHTTEXT

BASE

Converts a positive integer to a specified base into a text from the numbering system. The digits 0-9 and the letters A-Z are used.

语法

BASE(Number; Radix [; MinimumLength])

数字」是要转换的正整数。

Radix indicates the base of the numeral system. It may be any positive integer between 2 and 36.

MinimumLength」 (可选) 用于确定已创建的字符序列的最小长度。如果文字长度小于设置的最小长度,则会在字符串左边加零。

示例

=BASE(17;10;4)」返回十进制系统中的 0017。

=BASE(17;2)」返回二进制系统中的 10001。

=BASE(255;16;4)」返回十六进制系统中的 00FF。

See also

DECIMAL

CHAR

根据当前代码表将数字转换成字符。数字可以是两位或三位的整数。

大于 127 的代码会依赖于您系统的字符映射 (例如 iso-8859-1、iso-8859-2、Windows-1252、Windows-1250),因此可能不能移植。

语法

CHAR(数字)

数字」是一个表示字符代码值的介于 1 和 255 之间的数字。

示例

=CHAR(100)」返回字符 d。

="abc" & CHAR(10) & "def" 在字符串中插入换行符。

CLEAN

所有非打印字符都被从字符串中删除。

语法

CLEAN("Text")

Text」要从中删除所有不能打印的字符的文本。

示例

=LEN(CLEAN(CHAR(7) & "LibreOffice Calc" & CHAR(8))) returns 16, showing that the CLEAN function removes the non-printable Unicode U+0007 ("BEL") and U+0008 ("BS") characters at the beginning and end of the string argument. CLEAN does not remove spaces.

CODE

返回文本字符串中第一个字符的数字代码。

语法

CODE("Text")

Text」要获得第一个字符编码的文字。

大于 127 的代码会依赖于您系统的字符映射 (例如 iso-8859-1、iso-8859-2、Windows-1252、Windows-1250),因此可能不能移植。

示例

=CODE("Hieronymus")」返回 72,「=CODE("hieroglyphic")」返回 104。

note

在这里使用的编码并不是ASCII编码,而是当前编码表的编码。


CONCATENATE

将多个文本字符串合并为一个字符串。

语法

CONCATENATE(String 1 [; String 2 [; … [; String 255]]])

String 1[; String 2][; … ;[String 255]] are strings or references to cells containing strings.

示例

=CONCATENATE("Good ";"Morning ";"Mrs. ";"Doe")」返回: Good Morning Mrs. Doe。

DECIMAL

Converts text that represents a number in a numeral system with the given base radix to a positive integer. The radix must be in the range 2 to 36. Spaces and tabs are ignored. The Text field is not case-sensitive.

If the radix is 16, a leading x or X or 0x or 0X, and an appended h or H, are disregarded. If the radix is 2, an appended b or B is disregarded. Other characters that do not belong to the numeral system generate an error.

语法

DECIMAL("Text"; Radix)

Text is the text to be converted.

Radix indicates the base of the numeral system. It may be any positive integer between 2 and 36.

示例

=DECIMAL("17";10)」返回 17。

=DECIMAL("FACE";16)」返回 64206。

=DECIMAL("0101";2)」返回 5。

See also

BASE

DOLLAR

Converts a number to a string representing the amount in the currency format, rounded to a specified decimal places, using the decimal separator that corresponds to the current locale setting. In the Value field enter the number to be converted. Optionally, you may enter the number of decimal places in the Decimals field. If no value is specified, all numbers in currency format will be displayed with two decimal places.

在系统设置中设置货币格式。

语法

DOLLAR(Value [; Decimals])

」是一个数值,一个含有数值的单元格引用,或一个返回数值的公式。

Decimals」是小数的可选位数。

示例

=DOLLAR(255) returns $255.00 for the English (USA) locale and USD (dollar) currency; ¥255.00 for the Japanese locale and JPY (yen) currency; or 255,00 € for the German (Germany) locale and EUR (euro) currency.

=DOLLAR(367.456;2) returns $367.46.

EXACT

比较两个文本字符串,如果二者相同,则返回 TRUE。此函数区分大小写。

语法

EXACT("Text1"; "Text2")

Text1」是要比较的第一个文本。

Text2」是要比较的第二个文本。

示例

=EXACT("microsystems";"Microsystems")」返回 FALSE。

FIND

返回一个文本字符串在另一个字符串中的位置。您也可以指定查找的开始位置。查找的项可以是数字或任意字符的字符串。查找区分大小写。

语法

FIND("FindText"; "Text" [; Position])

FindText」是要查找的文字。

Text」是要进行搜索的文本。

Position」 (可选) 是文字中开始搜索的位置。

示例

=FIND(76;998877665544)」返回 6。

FIXED

Returns a number as text with a specified number of decimal places and optional thousands separators.

语法

FIXED(Number; [Decimals = 2 [; NoThousandsSeparators = FALSE]])

Number is rounded to Decimals places (after the decimal separator) and the result formatted as text, using locale-specific settings.

Decimals (optional) refers to the number of decimal places to be displayed. If Decimals is negative, Number is rounded to ABS(Decimals) places to the left from the decimal point. If Decimals is a fraction, it is truncated actually ignoring what is the closest integer.

NoThousandsSeparators (optional) determines whether the thousands separator is used. If it is TRUE or non-zero, then group separators are omitted from the resulting string. If the parameter is equal to 0 or if it is missing altogether, the thousands separators of your current locale setting are displayed.

示例

=FIXED(1234567.89;3)」将 1,234,567.890 作为文本字符串返回。

=FIXED(123456.789;;TRUE) returns 123456.79 as a text string.

=FIXED(12345.6789;-2) returns 12,300 as a text string.

=FIXED(12134567.89;-3;1) returns 12135000 as a text string.

=FIXED(12345.789;3/4) returns 12,346 as a text string.

=FIXED(12345.789;8/5) returns 12,345.8 as a text string.

LEFT

返回文本中第一个或前几个字符。

语法

LEFT("Text" [; Number])

文本」是确定初始部分字符的文本。

数字」 (可选) 指定开始文字的字符数。如果不定义此参数,则返回一个字符。

示例

=LEFT("output";3)」返回「out」。

LEFTB

返回 DBCS 文本的第一个字符。

tip

This function is available since LibreOffice 4.2.


语法

LEFTB("Text" [; Number_bytes])

文本」是需要确定其部分字节的原始文本,。

字节数」 (可选) 指定您需要用LEFTB截取的字符长度,单位为字节。如果该参数未指定,则返回1个字节。

示例

=LEFTB("中国";1) returns " " (1 byte is only half a DBCS character and a space character is returned instead).

=LEFTB("中国";2) returns "中" (2 bytes constitute one complete DBCS character).

=LEFTB("中国";3) returns "中 " (3 bytes constitute one DBCS character and a half; the last character returned is therefore a space character).

=LEFTB("中国";4) returns "中国" (4 bytes constitute two complete DBCS characters).

=LEFTB("office";3) returns "off" (3 non-DBCS characters each consisting of 1 byte).

LEN

返回字符串的长度 (包括空格)。

语法

LEN("Text")

Text」是需要确定长度的文本。

示例

=LEN("Good Afternoon")」返回 14。

=LEN(12345.67)」返回 8。

LENB

对于双字节字符集 (DBCS) 语言,返回用于表示文本字符串中的字符所使用的字节数。

tip

This function is available since LibreOffice 4.2.


语法

LENB("文本")

Text」是需要确定长度的文本。

示例

LENB("中")」返回 2 (1 个 DBCS 字符为 2 字节)。

LENB("中国")」返回 4 (2 个 DBCS 字符,每个字符由 2 个字节构成)。

LENB("office")」返回 6 (6 个非-DBCS 字符串,每个均为 1 字节)。

=LENB("Good Afternoon")」返回 14。

=LENB(12345.67)」返回 8。

LOWER

将文本字符串中的所有大写字母转换为小写字母。

语法

LOWER("Text")

Text」是要转换的文字。

示例

=LOWER("Sun")」返回 sun。

MID

返回文本的文本字符串。参数用于指定起始位置和字符数。

语法

MID("Text"; Start; Number)

Text」是包含要提取的字符的文本。

Start」是要从文字字符串中提取字符的起始位置。

数字」指定文字中字符的数目。

示例

=MID("office",2,2)」返回 ff。

MIDB

返回DBCS文本的一个文本字符串。参数用于指定起始位置以及要返回的字节数。

tip

This function is available since LibreOffice 4.2.


语法

MIDB("文本", 起始, 字节数)

文本」是包含有要提取的字符的文本。

起始」要提取的文本中第一个字符的位置。

字节数」指定了 MIDB 将要返回的字符数,以字节为单位。

示例

=MIDB("中国";1;0) returns "" (0 bytes is always an empty string).

=MIDB("中国";1;1) returns " " (1 byte is only half a DBCS character and therefore the result is a space character).

=MIDB("中国";1;2) returns "中" (2 bytes constitute one complete DBCS character).

=MIDB("中国";1;3) returns "中 " (3 bytes constitute one and a half DBCS character; the last byte results in a space character).

=MIDB("中国";1;4) returns "中国" (4 bytes constitute two complete DBCS characters).

=MIDB("中国";2;1) returns " " (byte position 2 is not at the beginning of a character in a DBCS string; 1 space character is returned).

=MIDB("中国";2;2) returns " " (byte position 2 points to the last half of the first character in the DBCS string; the 2 bytes asked for therefore constitutes the last half of the first character and the first half of the second character in the string; 2 space characters are therefore returned).

=MIDB("中国";2;3) returns " 国" (byte position 2 is not at the beginning of a character in a DBCS string; a space character is returned for byte position 2).

=MIDB("中国";3;1) returns " " (byte position 3 is at the beginning of a character in a DBCS string, but 1 byte is only half a DBCS character and a space character is therefore returned instead).

=MIDB("中国";3;2) returns "国" (byte position 3 is at the beginning of a character in a DBCS string, and 2 bytes constitute one DBCS character).

=MIDB("office";2;3) returns "ffi" (byte position 2 is at the beginning of a character in a non-DBCS string, and 3 bytes of a non-DBCS string constitute 3 characters).

PROPER

将文本字符串的所有单词的首字母转换成大写。

语法

PROPER("文本")

文本」要转换的文字。

示例

=PROPER("the document foundation") returns The Document Foundation.

REPLACE

将文本字符串的一部分用其他文本字符串替换。 此函数可用于替换字符和数字 (将自动转换为文本)。函数的结果将始终显示为文本类型。如果想对已经替换成文本的数字执行进一步的计算,必须使用VALUE 函数将其转换回数字。

输入含有数字的文字时,如果您希望输入的信息不被解释为数字并自动转换成文字,则需要在文字前后加上引号。

语法

REPLACE("文本", 位置, 长度, "新文本")

文本」要将其部分内容替换掉的文本。

位置」是要替换的文本的起始位置。

长度」是要在「文本」中替换掉的字符数目。

新文本 是用来替换文本」的文字。

示例

=REPLACE("1234567",1,1,"444")」返回 "444234567"。位于位置1的一个字符整个被「新文本」替代。

REPT

按指定的重复「次数」重复字符串。

语法

REPT("文本", 次数)

文本」是要重复的文字。

次数」是重复的次数。

示例

=REPT("Good morning",2)」返回 Good morningGood morning。

tip

Refer to the REPT wiki page for more details about this function.


RIGHT

返回文本中最后一个或多个字符。

语法

RIGHT("Text" [; Number])

文本」是指要确定其右侧部分字符的文字。

Number (optional) is the number of characters from the right part of the text. If this parameter is not defined, one character is returned.

示例

=RIGHT("Sun",2)」返回 un。

RIGHTB

返回双字节字符集 (DBCS) 文本的最后一个或几个字符。

tip

This function is available since LibreOffice 4.2.


语法

RIGHTB("Text" [; Number_bytes])

文本」是需要确定其右侧字符的文本。

Number_bytes (optional) specifies the number of characters you want RIGHTB to extract, based on bytes. If this parameter is not defined, one byte is returned.

示例

RIGHTB("中国";1)」返回「」 (1 个字节只是半个 DBCS 字符,因此返回空格字符作为代替)。

RIGHTB("中国";2)」返回「国」 (2 个字节构成一个完整的 DBCS 字符)。

RIGHTB("中国";3)」返回「国」 (3 个字节构成半个 DBCS 字符,和一个完整的 DBCS 字符; 第一个半字符返回空格)。

RIGHTB("中国";4)」返回「中国」 (4 个字节构成两个完整的 DBCS 字符)。

RIGHTB("office";3)」返回「ice」(3 个非 DBCS 字符,每个字符分别由一个字节构成)。

SEARCH

返回字符串中某个文本段的位置。您可以设置搜索的起始位置。搜索的文字可以是数字或任意的字符序列。搜索不区分大小写。如果文字未找到,则返回错误 519 (#VALUE)。

The search supports wildcards or regular expressions. With regular expressions enabled, you can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must either precede every regular expression metacharacter or operator with a "\" character, or enclose the text into \Q...\E. You can switch the automatic evaluation of wildcards or regular expression on and off in - LibreOffice Calc - Calculate.

warning

When using functions where one or more arguments are search criteria strings that represents a regular expression, the first attempt is to convert the string criteria to numbers. For example, ".0" will convert to 0.0 and so on. If successful, the match will not be a regular expression match but a numeric match. However, when switching to a locale where the decimal separator is not the dot makes the regular expression conversion work. To force the evaluation of the regular expression instead of a numeric expression, use some expression that can not be misread as numeric, such as ".[0]" or ".\0" or "(?i).0".


语法

SEARCH("FindText"; "Text" [; Position])

FindText」是要搜索的文本。

Text」是要在其中进行搜索的文本。

Position」 (可选) 是开始搜索的起始字符位置。

示例

=SEARCH(54;998877665544)」返回 10。

SUBSTITUTE

用新文字替换字符串中的旧文字。

语法

SUBSTITUTE("Text"; "SearchText"; "NewText" [; Occurrence])

Text」是需要交换部分文字的文本。

SearchText」是要 (多次) 替换的文本段。

NewText」是用于替换文本段的文本。

次数」 (可选) 指出要替换第几次出现的搜索文本。如果不指定此参数,则所有搜索文本都将被替换。

示例

=SUBSTITUTE("123123123";"3";"abc")」返回 12abc12abc12abc。

=SUBSTITUTE("123123123";"3";"abc";2)」返回 12312abc123。

T

此函数返回目标文本,或者在目标不是文本时返回空白文本字符串。

语法

T(值)

如果「」是 (或表示) 文本字符串,则 T 返回文本字符串;否则返回空文本字符串。

示例

=T(12345)」返回空字符串。

=T("12345")」返回字符串 12345。

TEXT

Converts a value into text according to a given format.

语法

TEXT(Value; Format)

Value is the value (numerical or textual) to be converted.

Format」是定义格式的文字。根据单元格格式中的语言设置来使用小数点和千位分隔符。

示例

=TEXT(12.34567;"###.##")」返回文本 12.35

=TEXT(12.34567;"000.00")」返回文本 012.35

=TEXT("xyz";"=== @ ===") returns the text === xyz ===

tip

参见 数字格式代码: 用户定义的自定义格式代码。


TRIM

删除字符串中的空格,在字词之间只留一个空格。

语法

TRIM("Text")

文本」是指想要去除其中空格的文本。

示例

=TRIM(" hello world ")」返回「hello world」, 不包含开头和结尾的空格,但是包含单词之间的空格。

UNICHAR

将代码数字转换为 Unicode 字符或字母。

语法

UNICHAR(数字)

示例

=UNICHAR(169) 返回版权字符「©」。

tip

参见 UNICODE() 函数。


UNICODE

返回文本字符串中第一个 Unicode 字符的数字代码。

语法

UNICODE("文本")

示例

=UNICODE("©") 返回版权字符的 Unicode 数字 169。

tip

参见 UNICHAR() 函数。


UPPER

将「文本」字段中指定的字符串转换为大写形式。

语法

UPPER("Text")

Text」是要将其转换为大写的小写字母。

示例

=UPPER("Good Morning")」返回 GOOD MORNING。

请支持我们!