【问题标题】:How to remove last space text clipboard applescript?如何删除最后一个空格文本剪贴板applescript?
【发布时间】:2017-10-07 13:45:49
【问题描述】:

原来是:

“你好 你好 你好 "

需要

“你好你好”

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

获取剪贴板 将剪贴板设置为(结果将“1”替换为“2”) 用 newDelim 替换 oldDelim 以获取 sourceString 将 oldTIDs 设置为 AppleScript 的文本项分隔符 将 AppleScript 的文本项分隔符设置为 oldDelim 将 strtoks 设置为 sourceString 的文本项 将 AppleScript 的文本项分隔符设置为 newDelim 将joinedString设置为strtoks作为字符串 将 AppleScript 的文本项分隔符设置为 oldTID 加入字符串 结束替换

【问题讨论】:

  • @alexander.polomodov,我相信您的编辑改变了最初的要求,即如何从“hello hello hello”中删除尾随空格,不是 b> 如何从“hello\nhello\nhello”获取hello hello hello

标签: applescript


【解决方案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

返回:

"hello hello hello" -- 带引号


如果你不想要引号

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 是公平的。 :)
  • 虽然您所做的编辑显然有效并且允许超过 OP 示例中的三个单词,但 IMO 您添加了不必要的代码行。我认为没有必要先计算字数,所有需要改变的只是set theWords to text from word 1 to word 3 of theStringset theWords to text from first word to last word of theString。这样一来,就不需要对单词进行编码了,后者在没有添加编码的情况下做同样的事情。
【解决方案2】:

假设剪贴板仅包含一行 text 就像您的问题一样,例如"hello hello hello",不带引号,下面一行去掉尾随空格。

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

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

【讨论】:

  • 该死的......而且我认为我的回答很棒 LOL +1
【解决方案3】:

这是一个将最后一个字符与空白列表进行比较的简洁方法。比一系列 if or's 更简洁。

-- 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”而不是“最后一个字符”会更快,但是这会忽略尾随的返回字符,因此对您不起作用)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2015-07-04
    • 1970-01-01
    相关资源
    最近更新 更多