Chinaunix首页 | 论坛 | 博客
  • 博客访问: 88616
  • 博文数量: 19
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 255
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-07 13:36
文章分类

全部博文(19)

文章存档

2011年(2)

2010年(1)

2009年(3)

2008年(13)

我的朋友

分类:

2008-06-19 10:16:10

TRANSLATE USING 的转换规则

从网上搜了一圈没找到满意答案,只好看英文帮助了

原文如下:

TRANSLATE

Syntax

TRANSLATE text {TO {UPPER|LOWER} CASE}
             | {USING pattern}.

Extras:



Effect

This statement converts the case or single characters of the character-type data object text. The statement CASE can be used for the conversion to upper/lower case; USING can be used for the conversion according to a pattern. The variable text must be character-type.

Note

There are two of this statement:

Addition 1

... TO {UPPER|LOWER} CASE

Effect

If you specify UPPER, all lower-case letters of the data object text are converted to upper case. If you specify LOWER, all upper-case letters are converted to lower case.

Note

The conversion of the upper/lower case depends on the . Problems may occur if the language of the text environment differs from the language in which the data to be processed is entered. Data loss will occur if a conversion between the source and target language has not been defined. To avoid this type of inconsistency, you must appropriately set the text environment using the statement prior to the conversion.

Example

After the conversion, the variable text contains "CAREFUL WITH THAT AXE, EUGENE".

DATA text TYPE string.
text = `Careful with that Axe, Eugene`.
TRANSLATE text TO UPPER CASE.

Addition 2

... USING  pattern

Effect

If you specify USING, the characters in text are converted according to the rule specified in pattern. pattern must be a character-type data object whose contents are interpreted as a sequence of character pairs. text is searched for the first character of each pair, starting with the first pair, and each location found is replaced with the second character of the pair. The search is case-sensitive. If pattern contains a character multiple times as the first character of a pair, only the first pair is taken into account. A character in text that has already been replaced cannot be replaced again in the same TRANSLATE statement. Therefore, if the second character of a pair in pattern appears as the first character of a subsequent pair, the second pair affects only the original characters in text.

Trailing blanks in data objects text and pattern are taken into account for data objects. If pattern contains an uneven number of characters, the last character is ignored. If pattern is a blank string, no replacements take place.

Example

Converts the characters "A" to "B", "a" to "b", and vice versa. text contains "Abracadabra" after the conversion.

DATA text TYPE string.
text = `Barbcbdbarb`.
TRANSLATE text USING 'ABBAabba'.

只把USING 部分翻译一下

  如果你使用了USING,那么text中的字符就要根据pattern 中指定的规则进行转换。Pattern必须是字符类型的数据对象并且它的内容是一个字符对的序列。从第一个字符对开始,用字符对的第一个字符遍历text,所有找到的地方都要用字符对的第二个字符替换。如果Pattern中字符对的第一个字符重复出现,则只做一次。text中已经被替换的字符在同一个TRANSLATE语句中不能被再次替换。因此,如果Pattern中字符对的第二个字符作为后来字符对的第一个字符出现,则第二个字符对只影响text中的初始字符。

   textPattern中末尾的空格也要考虑进去。如果Pattern中的字符数是奇数的话,最后一个字符则被忽略。如果Pattern是一个空字符串,则没有替换发生。

例子解释: 

DATA text TYPE string.
text = `Barbcbdbarb`.
TRANSLATE text USING 'ABBAabba'.

  其中

      text  =  `Barbcbdbarb` Pattern  =  'ABBAabba'

 Pattern 中共包含8个字符 4个字符对,分别是:’AB’,’BA’,’ab’,’ba’.

  遍历步骤:

1’AB’:搜索text中的 AB替换text没有变化;

2’BA’:搜索text中的‘BA替换,(因为‘B’是第一个字符对的第二个字符,所以只影响text中的初始字符。)替换后为’Aarbcbdbarb’;

3’ab’ 搜索text中的‘ba替换,替换后为’Aaracadaara’;

4. ’ ba’ 搜索text中的‘ab替换,(因为‘b’是第3个字符对的第二个字符,所以只影响text中的初始字符。)替换后为’Abracadabra.

 

阅读(5707) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~