在RPGLE编程中,在IBMi / iseries / AS400上如何将字符串转换为ProperCase / TitleCase?
按照惯例,我已经谷歌了我的$%#off并结束了Frankensteining我自己的解决方案。
这将在下面记录,因为我将不可避免地需要再次使用此proc。至少我知道谷歌会为我找到一个解决方案。
@FutureSelf =>谁就是这样!!!
答案 0 :(得分:3)
?*--------------------------------------------------
?* Procedure name: ConverToTitleCase
?*--------------------------------------------------
dcl-proc ConvertToTitleCase export;
dcl-pi *n varchar();
i_String varchar() const;
end-pi;
dcl-s o_String varchar();
dcl-s w_Text char();
dcl-s w_Word char();
dcl-s p_Word pointer;
dcl-s p_Last pointer;
dcl-c UPPER const(0);
dcl-c lower const(1);
dcl-pr GetToken pointer ExtProc('strtok_r');
String pointer value options(*string);
Seps pointer value options(*string);
Lasts pointer ;
end-pr;
dcl-c BLANK const(' ');
//-------------------------------------------------
if %trim(i_String) = *blanks;
return i_String; //Nothing to Convert;
endif;
//-------------------------------------------------
p_Word = GetToken(ConvertCase(i_String:lower):BLANK:p_Last);
dow p_Word <> *null;
w_Word = %str(p_Word);
%subst(w_Word:1:1) = ConvertCase(%subst(w_Word:1:1):UPPER);
o_String += BLANK + w_Word;
p_Word = GetToken(*NULL:BLANK:p_Last);
enddo;
//-------------------------------------------------
return o_String;
end-proc;
//***************************************************
dcl-proc ConvertCase;
dcl-pi *n varchar();
i_String varchar() const;
i_CaseTo int(5) const;
end-pi;
dcl-s o_String varchar();
//Control Block for the call to QglConvertCase.
dcl-ds FormatOfRequestControlBlock;
ReqType int(10) inz(1); //Convert String
CCSID int(10) inz(0); //CCSID of JOB
CaseTo int(10) inz(0); //Default Lower
Reserved char(10) inz(*ALLX'00');
end-ds;
dcl-s APIError char(16) inz(*ALLX'00');
dcl-pr QlgCvtCase ExtProc('QlgConvertCase');
ctrlBlock LikeDS(FormatOfRequestControlBlock) Const;
inString char(65535) Options(*VARSIZE) Const;
OutString char(65535) Options(*VARSIZE);
Length int(10) Const;
APIError like(APIError);
end-pr;
if %len(%trim(i_String)) <= 0;
return i_String; //Noting to convert;
endif;
FormatOfRequestControlBlock.CaseTo = i_CaseTo;
QlgCvtCase(FormatOfRequestControlBlock
:i_String
:o_String
:%len(i_String)
:APIError);
return %Subst(o_String:1:%Len(i_String));
end-proc;
//***************************************************