是否有一种方法可以用特定字符格式化正在进行中的字符串?
一个例子是为前6个数字显示带有x's的SSN。我尝试使用String函数,但它不尊重随格式发送的字母x。
SSNString = '333224444'.
SSNString = String(SSNString, "xxx-xx-9999").
//This displays 333-22-4444 instead of xxx-xx-4444.发布于 2015-03-23 17:50:30
我不知道用字符串函数改变这种格式的方法。
如果只想保留最后四位数字,可以尝试这样的方法:
SSNString = '333224444'.
SSNString = "xxx-xx-" + SUBSTRING(SSNString, 6, 4).发布于 2015-03-24 20:42:40
STRING函数参数仅用于将输出字符串格式化为所需的模式,而不是“动态”替换字符。您需要一个CHAR变量来将数据库值放入其中,并且可以使用OVERLAY或SUBSTRING函数覆盖字符串中的前五个字符,以匹配您的条件。在你的例子中:
SSNString = '333224444'.
/* Choose the OVERLAY function or SUBSTRING function. You can pass either
a FILL function call, one variable or a fixed string value to OVERLAY
or SUBSTRING function call. They'll behave exactly the same way. */
OVERLAY(SSNString,1,5,'CHAR') = FILL('X',5).
SUBSTRING(SSNString,1,5) = 'XXXXX'.
/* At this point, SSNString is XXXXX4444 */
SSNString = STRING(SSNString,'XXX-XX-9999').
/* Here you'll have XXX-XX-4444 */
MESSAGE SSNString
VIEW-AS ALERT-BOX INFO BUTTONS OK.以一种更复杂和灵活的方式,您可以使用一个函数,该函数返回您想要的值,如果您想要将特殊格式应用于数据库字段值,则不需要使用变量。
FUNCTION formatSSNString RETURNS CHAR
( INPUT pSourceString AS CHAR,
INPUT pFormat AS CHAR,
INPUT pOverlayCount AS INT,
INPUT pStart AS INT,
INPUT pCharOverlay AS CHAR ):
DEF VAR cCharOutput AS CHAR NO-UNDO.
cCharOutput = pSourceString.
OVERLAY(cCharOutput,pStart,pOverlayCount,'CHAR') = FILL(pCharOverlay,pOverlayCount).
cCharOutput = STRING(cCharOutput,pFormat).
RETURN cCharOutput.
END FUNCTION.
DEF VAR SSNString AS CHAR NO-UNDO.
SSNString = '333224444'.
/* This returns XXX-XX-4444 */
MESSAGE formatSSNString(SSNString, 'xxx-xx-9999', 5, 1, 'X')
VIEW-AS ALERT-BOX.
/* This returns 33X-XX-X444 */
MESSAGE formatSSNString(SSNString, 'xxx-xx-9999', 4, 3, 'X')
VIEW-AS ALERT-BOX.
/* This returns 333-XX-4444 */
MESSAGE formatSSNString(SSNString, 'xxx-xx-9999', 2, 4, 'X')
VIEW-AS ALERT-BOX.
/* This returns 333-XX-44YY */
MESSAGE formatSSNString(formatSSNString(SSNString, 'x(9)', 2, 4, 'X'), 'xxx-xx-9999', 2, 8, 'Y')
VIEW-AS ALERT-BOX.希望能帮上忙。
https://stackoverflow.com/questions/29216231
复制相似问题