我想从字符串的正面和背面修剪' - ',而不是在中间。所以:
-Some-thing- = Some-thing
-Another = Another
Some-thing = Some-thing
One more- = One more
etc.
TRIM功能是否允许您这样做?
答案 0 :(得分:0)
如果它总是只是一个连字符在结尾或单词的开头悬挂,那么你可以使用一些if()
功能执行此操作:
=IF(LEFT(A1, 1) = "-", RIGHT(A1, LEN(A1)-1), IF(RIGHT(A1, 1)="-", LEFT(A1, LEN(A1)-1)))
如果需要删除多个连字符,我认为最好的选择是使用UDF:
Function TrimCharacter(inString As String, trimChar As String) As String
For Each word In Split(inString, trimChar)
If Len(word) > 0 Then TrimCharacter = TrimCharacter & IIf(TrimCharacter = "", word, trimChar & word)
Next word
End Function
只需将其放入VBE中的新模块中,保存工作簿,然后就可以在单元格公式中使用它:
=TrimCharacter(A1, "-")
它会从单词的开头和结尾修剪所有连字符。