【问题标题】:Remove white spaces from BrightScript string从 BrightScript 字符串中删除空格
【发布时间】:2018-12-01 04:38:30
【问题描述】:

我正在尝试使用正则表达式从字符串中删除前导空格和尾随空格

regexQuote = CreateObject("roRegex", "/^[ ]+|[ ]+$/g+", "i")
regexQuote.ReplaceAll(noSpaceString)
print noSpaceString

[编辑]

regexQuote = CreateObject("roRegex", "/^[ ]+|[ ]+$/g", "")
print len(noSpaceString) //this value includes leading white spaces, which I dont want

我也试过了

regexQuote = CreateObject("roRegex", "/^[ ]+|[ ]+$/", "")

尝试过

regexQuote = CreateObject("roRegex", "/(^\s*)|(\s*$)/", "")

【问题讨论】:

  • g+i 是否有意义?我的第一个想法是删除 i 标志,因为您匹配的是空白而不是 alpha,并删除 g 之后的 +,除非它是我不熟悉的一些奇怪的亮脚本。
  • @tenub 我更新问题
  • 你试过了吗:/(^\s*)|(\s*$)/
  • 你以前在这个平台上使用过正则表达式吗?快速查看 docs 告诉我,您不必像在 PHP 中那样在正则表达式字符串中添加分隔符,并且您不需要 g 修饰符,因为 ReplaceAll() 函数就像它所说的那样: 替换所有匹配项。
  • 你能试试吗:print len(regexQuote.ReplaceAll(noSpaceString)) 我认为 ReplaceAll 返回一个新字符串并且不会改变原来的字符串

标签: brightscript


【解决方案1】:

使用trim()

使用trim(),卢克!有一个字符串方法只是为了这个目的:

BrightScript Debugger> ? len("   four   ".trim())
 4

【讨论】:

    【解决方案2】:

    从 Roku 的 ifString 操作中,您可以使用 Replace 作为:

    newString = originalString.Replace(" ", "")
    

    【讨论】:

      【解决方案3】:

      在这里使用评论部分的帮助是解决方案

      regexQuote = CreateObject("roRegex", "^\s+|\s+$", "")
      newString= regexQuote.ReplaceAll(oldString, "")
      print "string length:" ; len(newString)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-21
        • 2017-11-20
        • 1970-01-01
        • 2012-03-25
        相关资源
        最近更新 更多