如何删除最后一个空格文本剪贴板applescript?

时间:2017-10-07 13:45:49

标签: applescript

是:

"你好 你好 你好 "

需要

"你好你好你好"

如果删除最后一个字符空格 谢谢大家!

get the clipboard
set the clipboard to (replacement of "1" by "2" for the result)

on replacement of oldDelim by newDelim for sourceString
    set oldTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to oldDelim
    set strtoks to text items of sourceString
    set text item delimiters of AppleScript to newDelim
    set joinedString to strtoks as string
    set text item delimiters of AppleScript to oldTIDs
    joinedString
end replacement

3 个答案:

答案 0 :(得分:1)

set the clipboard to "hello hello hello "
set theString to get the clipboard
set theWords to text from first word to last word of theString
set the clipboard to quote & theWords & quote

返回值:

"你好你好你好" - 带引号

如果您不想要引号

set the clipboard to "hello hello hello "
set theString to get the clipboard
set theWords to text from first word to last word of theString
set the clipboard to theWords 

返回值:

你好你好你好 - 没有引号

答案 1 :(得分:1)

假设剪贴板中只包含一行 text ,例如问题,例如"你好你好你好",没有引号,下面一个衬里删除了尾随空格。

set the clipboard to text from first word to last word of (the clipboard as text)

注意:这也会删除任何数量的前导和尾随空格,并且剪贴板中的单行文本所包含的文本字数不受限制。

答案 2 :(得分:-1)

这是一个简洁的方法,可以将最后一个字符与空格列表进行比较。比一系列if或&#39>更清洁。

-- set clipboard to "hello hello hello       "
set theString to the clipboard
repeat 99 times
    set c to last character of theString
    if c is in {tab, space, return} then
        set theString to text 1 thru -2 of theString
    else
        exit repeat
    end if
end repeat
return theString

(它在技术上更快地抓取"文本-2到-1的字符串"而不是"最后一个字符"但是这将忽略尾随返回字符,所以赢了&# 39;为你工作)

相关问题