代码之家  ›  专栏  ›  技术社区  ›  Piszu

清除空格中的字符串powerbuilder

  •  0
  • Piszu  · 技术社区  · 12 年前

    我有绳子

    'TEST1, TEST2, TEST3'
    

    我想要

    'TEST1,TEST2,TEST3'
    

    powerbuilder中是否有类似replace、substr之类的函数?

    3 回复  |  直到 12 年前
        1
  •  2
  •   RealHowTo    12 年前

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

    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
  •  2
  •   Seki    12 年前

    当然有(你可以很容易地在帮助中找到它),但它并没有太大帮助。

    它的原型是 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中的字符串函数(搜索和连接)效率不高,另一种解决方案可能是使用 FastReplaceall() 由提供的全局功能 PbniRegex 扩大它是一个c++编译的插件,适用于9到12版本的PB classic。

        3
  •  0
  •   Piszu    12 年前

    我这样做:

      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