我有一个字符串如下:
set str = "HELLO SO COMMUNITY| CAN YOU HELP ME"
foreach word ($str)
echo $word
end
目前,这打印HELLO然后是SO然后是COMMUNITY等等。 我希望打印的分隔符为| 。所以输出应该是HELLO SO COMMUNITY然后你可以帮助我。有谁知道怎么做。
答案 0 :(得分:0)
set str = "HELLO SO COMMUNITY| CAN YOU HELP ME"
set delimiter = '|'
set special_char = '_' # A character you know doesn't appear in your strings
set words = `echo $str | sed "s/ /$special_char/g" | sed "s/$delimiter/ /g"`
# words now contains "HELLO_SO_COMMUNITY _CAN_YOU_HELP_ME"
foreach word ($words)
echo $word | sed "s/$special_char/ /g"
end
将special_char
替换为您知道字符串中未出现的字符。小心任何特殊字符。我会避免使用*
,?
,$
,\
以及可能由cshell解释的其他令牌。 cshell的规则#1是总是以错误的方式解释它们......:P