【问题标题】:Clear string from spaces powerbuilder从空间 powerbuilder 清除字符串
【发布时间】:2012-09-25 07:45:09
【问题描述】:

我有字符串

'TEST1, TEST2, TEST3'

我想拥有

'TEST1,TEST2,TEST3'

powerbuilder里面是不是有replace、substr之类的函数?

【问题讨论】:

    标签: string powerbuilder


    【解决方案1】:

    一种方法是使用数据库,因为您可能有一个活动连接。

    string ls_stringwithspaces = "String String String     String"
    string ls_stringwithnospace = ""
    string ls_sql = "SELECT  replace('" + ls_stringwithspaces + "', ' ', '')"
    
    DECLARE db DYNAMIC CURSOR FOR SQLSA;
    PREPARE SQLSA FROM :ls_sql USING SQLCA;
    
    OPEN DYNAMIC db;
    IF SQLCA.SQLCode > 0 THEN
           // erro handling
    END IF
    FETCH  db INTO :ls_stringwithnospace;
    CLOSE db;
    
    MessageBox("", ls_stringwithnospace)
    

    【讨论】:

      【解决方案2】:

      当然有(您可以在帮助中轻松找到它),但它并不是很有帮助。

      它的原型是Replace ( string1, start, n, string2 ),所以在调用它之前需要知道要替换的字符串的位置。

      对此有一个通用的包装器,包括循环 pos() / replace() 直到没有任何东西可以替换。下面是一个全局函数的源码:

      global type replaceall from function_object
      end type
      
      forward prototypes
      global function string replaceall (string as_source, string as_pattern, string as_replace)
      end prototypes
      
      global function string replaceall (string as_source, string as_pattern, string as_replace);//replace all occurences of as_pattern in as_source by as_replace 
      
      string ls_target
      long i, j
      
      ls_target=""
      i = 1
      j = 1
      do
          i = pos( as_source, as_pattern, j )
          if i>0 then
              ls_target += mid( as_source, j, i - j )
              ls_target += as_replace
              j = i + len( as_pattern )
          else
              ls_target += mid( as_source, j )
          end if
      loop while i>0
      
      return ls_target
      
      end function
      

      请注意,PB 中的字符串函数(搜索和连接)效率不高,另一种解决方案是使用 PbniRegex 扩展提供的 FastReplaceall() 全局函数。它是一个c++编译的PB Classic 9到12版本的插件。

      【讨论】:

        【解决方案3】:

        我这样做:

          long space, ll_a
             FOR ll_a = 1 to len(ls_string)
                    space = pos(ls_string, " ")
                    IF space > 0 THEN
                    ls_string= Replace(ls_string, space, 1, "")
                    END IF
             NEXT
        

        【讨论】:

        • 那么,如果您的源字符串不包含任何空格,您仍然在字符串上循环len(ls_string) 次,每次调用pos()?网址!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-07
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多