【问题标题】:stringByReplacingOccurrencesOfString: in AppleScript Obj-CstringByReplacingOccurrencesOfString:在 AppleScript Obj-C 中
【发布时间】:2012-08-16 11:04:00
【问题描述】:

有什么方法可以在 AppleScript Obj-C 中使用下面的方法或类似的方法?

NSString *request = [urlString stringByReplacingOccurrencesOfString:@"{query}"
                                                        withString:queryString];

【问题讨论】:

标签: objective-c cocoa nsstring applescript


【解决方案1】:

您可以使用文本项目分隔符

set theReplacementString to "-----"
set {theOriginal, text item delimiters} to {text item delimiters, "{query}"}
set theParts to every text item of "{query}asd{query}sadsa{query}"
set text item delimiters to theReplacementString
set theResult to theParts as string
set text item delimiters to theOriginal
return theResult

给你

"-----asd-----sadsa-----"

【讨论】:

    【解决方案2】:

    这是有效的代码:

    set theText to "01 01 2005"
    set AppleScript's text item delimiters to " "
    set theTextItems to text items of theText
    set AppleScript's text item delimiters to "/ "
    set theText to theTextItems as string
    set AppleScript's text item delimiters to {""}
    theText
    

    【讨论】:

    • text item delimiters 属性不再是全局的,因此通常不需要恢复它。默认范围内也不需要AppleScript's 部分。
    【解决方案3】:

    要回答您问题的第一部分,您可以转换 Cocoa 方法并使用等效的 ASOC 语句(参见AppleScriptObjC Release Notes):

    set urlString to current application's NSString's stringWithString_("{query}asd{query}sadsa{query}") -- create an NSString to use with the Cocoa method
    set queryString to "-----" -- the replacement string
    set request to urlString's stringByReplacingOccurrencesOfString_withString_("{query}", queryString) -- the NSString method in ASOC
    display dialog request as text
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2019-10-10
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多