如何在 OpenEdge ABL / Progress 4GL 中将字符串转换为标题大小写?

发布于 2024-11-02 19:03:16 字数 273 浏览 1 评论 0原文

如何在 OpenEdge ABL(又名 Progress 4GL)中将字符串转换为标题大小写?

我知道我可以使用 CAPS() 获得大写字母,使用 LC() 获得小写字母,但我找不到标题大小写(有时称为正确大小写)函数。

示例:

Input           Output
------------    ------------
hello world!    Hello World!
HELLO WORLD!    Hello World!

How do I convert a string to title case in OpenEdge ABL (aka Progress 4GL)?

I know I can get upper case with CAPS(), and lower case with LC(), but I can't find the title case (sometimes called proper case) function.

Examples:

Input           Output
------------    ------------
hello world!    Hello World!
HELLO WORLD!    Hello World!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヅ她的身影、若隐若现 2024-11-09 19:03:16
function titleWord returns character ( input inString as character ):
  return caps( substring( inString, 1, 1 )) + lc( substring( inString, 2 )).
end.

function titleCase returns character ( input inString as character ):

  define variable i as integer no-undo.
  define variable n as integer no-undo.

  define variable outString as character no-undo.

  n = num-entries( inString, " " ).
  do i = 1 to n:
    outString =
      outString +
      ( if i > 1 and i <= n then " " else "" ) +
      titleWord( entry( i, inString, " " ))
    .
  end.

  return outString.

end.

display
  titleCase( "the quick brown fox JUMPED over the lazy dog!" ) format "x(60)"
.
function titleWord returns character ( input inString as character ):
  return caps( substring( inString, 1, 1 )) + lc( substring( inString, 2 )).
end.

function titleCase returns character ( input inString as character ):

  define variable i as integer no-undo.
  define variable n as integer no-undo.

  define variable outString as character no-undo.

  n = num-entries( inString, " " ).
  do i = 1 to n:
    outString =
      outString +
      ( if i > 1 and i <= n then " " else "" ) +
      titleWord( entry( i, inString, " " ))
    .
  end.

  return outString.

end.

display
  titleCase( "the quick brown fox JUMPED over the lazy dog!" ) format "x(60)"
.
安穩 2024-11-09 19:03:16

我认为上述语句之一的顺序不正确 -

您将在字符串的开头添加一个额外的“”!还需要将 <= 更改为 <或者您将在返回字符串中添加一个额外的“”。

应该是:

n = num-entries( inString, " " ).
  do i = 1 to n:
    outString =
      outString +
      titleWord( entry( i, inString, " " )) +
      ( if i < n then " " else "" ) +
    .
  end.

至少我-认为-应该是这样...-

I think the order of one of those statements above is incorrect -

You'll be adding an extra " " at the beginning of the string! Also need to change the <= to < or you'll be tacking an extra " " into your return string.

It should be:

n = num-entries( inString, " " ).
  do i = 1 to n:
    outString =
      outString +
      titleWord( entry( i, inString, " " )) +
      ( if i < n then " " else "" ) +
    .
  end.

At least that's what I -think- it should be...

-Me

毁我热情 2024-11-09 19:03:16

不久前我正在研究这个问题,除了类似于汤姆的解决方案之外,我还想出了两种变体。

我遇到的问题之一是并非所有单词都用空格分隔,例如运行时和读/写,因此我编写此版本以使用任何非字母字符作为分隔符。

我还想将变音符号和重音字符算作字母,所以它变得有点复杂。为了解决这个问题,我创建了两个版本的标题,一种大写,一种小写。如果两个字符串相同,则它是非字母字符,如果两个字符串不同,则它是字母字符。标题通常很短,因此这种方法并不像乍看起来那么低效。

FUNCTION TitleCase2 RETURNS CHARACTER
  ( pcText AS CHARACTER ) :
/*------------------------------------------------------------------------------
  Purpose: Converts a string to Title Case.  
    Notes: This version takes all non-alphabetic characters as word seperators
           at the expense of a little speed. This affects things like
           D'Arby vs D'arby or Week-End vs Week-end.
------------------------------------------------------------------------------*/

  DEFINE VARIABLE cUText AS CHARACTER   NO-UNDO CASE-SENSITIVE.
  DEFINE VARIABLE cLText AS CHARACTER   NO-UNDO CASE-SENSITIVE.

  DEFINE VARIABLE i      AS INTEGER     NO-UNDO.
  DEFINE VARIABLE lFound AS LOGICAL     NO-UNDO INITIAL TRUE.

  cUText = CAPS(pcText).
  cLText = LC(pcText).
  DO i = 1 TO LENGTH(pcText):
    IF (SUBSTRING(cUText, i, 1)) <> (SUBSTRING(cLText, i, 1)) THEN
    DO:
      IF lFound THEN
      DO:
         SUBSTRING(cLText, i, 1) = (SUBSTRING(cUText, i, 1)).
         lFound = FALSE.
      END.
    END.
    ELSE lFound = TRUE.
  END.
  RETURN cLText.   

END FUNCTION.

另一个问题是标题大小写应该是特定于语言的,即动词和名词与介词和连词的处理方式不同。以下是标题大小写的一些可能规则:

  1. 第一个和最后一个单词始终大写
  2. 所有名词、动词(包括“is”和其他形式的“to”)大写
    be”)、副词(包括“than”和“when”)、形容词(包括
    “这个”和“那个”)和代词(包括“它”)。
  3. 将属于动词短语一部分的介词大写。
  4. 小写冠词 (a, an, the)。
  5. 小写并列连词(and、but、for、nor、or)。
  6. 四个或更少字母的小写介词。
  7. 不定式短语中小写的“to”。
  8. 如果复合词中的第二个单词是名词或,则将其大写
    适当的形容词或单词具有同等的权重(交叉引用,
    Microsoft 之前的软件、读/写访问、运行时)。小写
    第二个词(如果是另一个词性或分词)
    修改第一个词(操作方法、起飞)。

当然,如果不教授计算机英语,我就无法编写所有这些代码,因此我创建了这个版本作为一个简单但粗略的妥协;它在大多数情况下都有效,但也有例外。

FUNCTION TitleCaseE RETURNS CHARACTER
  ( pcText AS CHARACTER ) :
/*------------------------------------------------------------------------------
  Purpose: Converts an English string to Title Case.  
    Notes:          
------------------------------------------------------------------------------*/

  DEFINE VARIABLE i           AS INTEGER     NO-UNDO.
  DEFINE VARIABLE cWord       AS CHARACTER   NO-UNDO.
  DEFINE VARIABLE lFound      AS LOGICAL     NO-UNDO INITIAL TRUE.
  DEFINE VARIABLE iLast       AS INTEGER     NO-UNDO.

  DEFINE VARIABLE cSmallWords AS CHARACTER   NO-UNDO
     INITIAL "and,but,or,for,nor,the,a,an,to,amid,anti,as,at,but,by,down,from,in" + 
             ",into,like,near,of,off,on,onto,over,per,than,to,up,upon,via,with".

  pcText = REPLACE(REPLACE(LC(pcText),"-"," - "),"/"," / ").
  iLast = NUM-ENTRIES(pcText, " ").
  DO i = 1 TO iLast:
    cWord = ENTRY(i, pcText, " ").
    IF LENGTH(cWord) > 0 THEN
      IF i = 1 OR i = iLast OR LOOKUP(cWord, cSmallWords) = 0 THEN
        ENTRY(i, pcText, " ") = CAPS(SUBSTRING(cWord, 1, 1)) + LC(SUBSTRING(cWord, 2)).
  END.

  RETURN REPLACE(REPLACE(pcText," - ","-")," / ","/").   

END FUNCTION.

我不得不提的是,汤姆的解决方案比我的解决方案快得多。根据您的需要,您可能会发现速度并不那么重要,因为您不太可能在大型数据处理过程或长字符串中使用它,但我不会忽略它。确保您的需求证明性能损失是合理的。

I was playing around with this a while back, and besides a solution similar to Tom's, I came up with two variations.

One of the problems I had was that not all words are separated by space, such as Run-Time and Read/Write, so I wrote this version to use any non-alphabetic characters as separators.

I also wanted to count diacritics and accented characters as alphabetic, so it became a little complicated. To solve the problem I create two versions of the title, one upper and one lower case. Where the two strings are the same, it's a non-alphabetic character, where they are different, it's alphabetical. Titles are usually very short, so this method is not as inefficient as might seem at first.

FUNCTION TitleCase2 RETURNS CHARACTER
  ( pcText AS CHARACTER ) :
/*------------------------------------------------------------------------------
  Purpose: Converts a string to Title Case.  
    Notes: This version takes all non-alphabetic characters as word seperators
           at the expense of a little speed. This affects things like
           D'Arby vs D'arby or Week-End vs Week-end.
------------------------------------------------------------------------------*/

  DEFINE VARIABLE cUText AS CHARACTER   NO-UNDO CASE-SENSITIVE.
  DEFINE VARIABLE cLText AS CHARACTER   NO-UNDO CASE-SENSITIVE.

  DEFINE VARIABLE i      AS INTEGER     NO-UNDO.
  DEFINE VARIABLE lFound AS LOGICAL     NO-UNDO INITIAL TRUE.

  cUText = CAPS(pcText).
  cLText = LC(pcText).
  DO i = 1 TO LENGTH(pcText):
    IF (SUBSTRING(cUText, i, 1)) <> (SUBSTRING(cLText, i, 1)) THEN
    DO:
      IF lFound THEN
      DO:
         SUBSTRING(cLText, i, 1) = (SUBSTRING(cUText, i, 1)).
         lFound = FALSE.
      END.
    END.
    ELSE lFound = TRUE.
  END.
  RETURN cLText.   

END FUNCTION.

Another issue is that title case is supposed to be language specific, i.e. verbs and nouns are treated differently to prepositions and conjunctions. These are some possible rules for title case:

  1. First and last word always get capitalized
  2. Capitalize all nouns, verbs (including "is" and other forms of "to
    be"), adverbs (including "than" and "when"), adjectives (including
    "this" and "that"), and pronouns (including "its").
  3. Capitalize prepositions that are part of a verb phrase.
  4. Lowercase articles (a, an, the).
  5. Lowercase coordinate conjunctions (and, but, for, nor, or).
  6. Lowercase prepositions of four or fewer letters.
  7. Lowercase "to" in an infinitive phrase.
  8. Capitalize the second word in compound words if it is a noun or
    proper adjective or the words have equal weight (Cross-Reference,
    Pre-Microsoft Software, Read/Write Access, Run-Time). Lowercase the
    second word if it is another part of speech or a participle
    modifying the first word (How-to, Take-off).

I could of course not code all this without teaching the computer English, so I created this version as a simple if crude compromise; it works in most cases, but there are exceptions.

FUNCTION TitleCaseE RETURNS CHARACTER
  ( pcText AS CHARACTER ) :
/*------------------------------------------------------------------------------
  Purpose: Converts an English string to Title Case.  
    Notes:          
------------------------------------------------------------------------------*/

  DEFINE VARIABLE i           AS INTEGER     NO-UNDO.
  DEFINE VARIABLE cWord       AS CHARACTER   NO-UNDO.
  DEFINE VARIABLE lFound      AS LOGICAL     NO-UNDO INITIAL TRUE.
  DEFINE VARIABLE iLast       AS INTEGER     NO-UNDO.

  DEFINE VARIABLE cSmallWords AS CHARACTER   NO-UNDO
     INITIAL "and,but,or,for,nor,the,a,an,to,amid,anti,as,at,but,by,down,from,in" + 
             ",into,like,near,of,off,on,onto,over,per,than,to,up,upon,via,with".

  pcText = REPLACE(REPLACE(LC(pcText),"-"," - "),"/"," / ").
  iLast = NUM-ENTRIES(pcText, " ").
  DO i = 1 TO iLast:
    cWord = ENTRY(i, pcText, " ").
    IF LENGTH(cWord) > 0 THEN
      IF i = 1 OR i = iLast OR LOOKUP(cWord, cSmallWords) = 0 THEN
        ENTRY(i, pcText, " ") = CAPS(SUBSTRING(cWord, 1, 1)) + LC(SUBSTRING(cWord, 2)).
  END.

  RETURN REPLACE(REPLACE(pcText," - ","-")," / ","/").   

END FUNCTION.

I have to mention that Tom's solution is very much faster than both of mine. Depending on what you need, you may find that the speed is not that important, since you're unlikely to use this in large data crunching processes or with long strings, but I wouldn't ignore it. Make sure that your needs justify the performance loss.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文