如何去除字符串中的重复字符?
How to remove duplicate characters in string?
我有一个看起来像这样的字符串:some, , , , text
我需要它看起来像这样:some, text
不知道有多少这样的逗号,可能是2个,也可能是10个
我尝试了递归方法,但它没有按预期工作:
<xsl:template name="remove-duplicate">
<xsl:param name="text" />
<xsl:param name="remove" />
<xsl:value-of select="$text" />
<xsl:if test="contains($text, $remove)">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="$text" />
<xsl:with-param name="replace" select="$remove" />
<xsl:with-param name="by" select="''" />
</xsl:call-template>
<xsl:call-template name="remove-duplicate">
<xsl:with-param name="text" select="$text" />
<xsl:with-param name="remove" select="$remove" />
</xsl:call-template>
</xsl:if>
</xsl:template>
调用模板:
<xsl:call-template name="remove-duplicate">
<xsl:with-param name="text" select="string(.)" />
<xsl:with-param name="remove" select="' ,'" />
</xsl:call-template>
string-replace-all
被盗自这个问题:XSLT string replace
怎么样:
<xsl:value-of select="translate(normalize-space(translate($yourString, ', ', ' …')), ' …', ', ')"/>
请注意,这假设您的字符串不包含除空格之外的任何空白字符。
如果此假设不成立,请尝试此处发布的解决方案:
我有一个看起来像这样的字符串:some, , , , text
我需要它看起来像这样:some, text
不知道有多少这样的逗号,可能是2个,也可能是10个
我尝试了递归方法,但它没有按预期工作:
<xsl:template name="remove-duplicate">
<xsl:param name="text" />
<xsl:param name="remove" />
<xsl:value-of select="$text" />
<xsl:if test="contains($text, $remove)">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="$text" />
<xsl:with-param name="replace" select="$remove" />
<xsl:with-param name="by" select="''" />
</xsl:call-template>
<xsl:call-template name="remove-duplicate">
<xsl:with-param name="text" select="$text" />
<xsl:with-param name="remove" select="$remove" />
</xsl:call-template>
</xsl:if>
</xsl:template>
调用模板:
<xsl:call-template name="remove-duplicate">
<xsl:with-param name="text" select="string(.)" />
<xsl:with-param name="remove" select="' ,'" />
</xsl:call-template>
string-replace-all
被盗自这个问题:XSLT string replace
怎么样:
<xsl:value-of select="translate(normalize-space(translate($yourString, ', ', ' …')), ' …', ', ')"/>
请注意,这假设您的字符串不包含除空格之外的任何空白字符。
如果此假设不成立,请尝试此处发布的解决方案: