【问题标题】:trim zeros from (substring of) nsString in xpcom从xpcom中的nsString(子字符串)修剪零
【发布时间】:2015-03-05 21:36:49
【问题描述】:

我正在尝试从包含字母数字(例如 ABC13、09889 等)的字符串的子字符串中删除前导零(如果有)。

这是我正在尝试的示例代码。不确定哪种类型的字符串适合此目的,因此使用 nsString。

nsString logicId;
    // this is nice way to assing values to nsString, mozilla way.
    logicId.Assign(NS_ConvertASCIItoUTF16((v)));// v is my value such as ABC786

    if(logicId.Length() > 0)
    {
        nsAString& lastFive = Substring(logicId, 17, 5);
        // lastFive is now a string representing the last 5 characters
        // let's trim leading zeros.
            lastFive.Trim("0", true, false);
        plugin->mId.Assign(lastFive);
    }

由于子字符串只是一个 const 指针,因此您无法对其进行修剪。那么如何获取子字符串并仍然对其进行修剪。有什么建议吗?

【问题讨论】:

    标签: firefox firefox-addon xpcom gecko xulrunner


    【解决方案1】:

    我设法做到了:

    nsString logicId;
    
    logicId.Assign(NS_ConvertASCIItoUTF16((v)));
    
            /* mozilla style */
            if(logicId.Length() > 0)
            {
    
                nsString lastFive(Substring(logicId, 17, 5));
                //and then trim that
                lastFive.Trim("0", true, false);
    
                plugin->mId.Assign(lastFive);
                //printf("%s\n", NS_ConvertUTF16toUTF8(lastFive).get());
    
            }
    

    【讨论】:

    • 感谢您的关注!这是 C++ 吧?
    • 它的 c++ 在 xpcom 组件中。在 xulrunner 的世界中。不再推荐使用 xulrunner,您现在应该在编写组件时使用 firefox。但是是的,您可以在 firefox 中使用 c++ 和 js 编写组件。
    • 嗨,不,它不是我的附加组件。但是你可以在网上找到关于在 firefox 中编写 c++ 插件的教程。我不知道开发人员是否仍然这样做。有一个叫做 jpm 的新东西,你可以用它来在你的插件中添加节点模块。我认为这提供了更大的灵活性,因为您可以在 js/c++ 中编写模块并从 jpm 调用它。
    • 首先尝试编写简单的 c++ 代码并从节点模块中调用它们,如 c++ node module 中所示。然后,如果你的模块工作正常......让我们说一个简单的 hello world,然后尝试从 firefox jpm 调用它
    • C++ 不会在 firefox 的范围之外执行吗?我需要在上下文中执行代码,特别是我需要在最近使用的窗口上执行XChangeProperty,我将从nativeHandle 获得最新的句柄:来自nsIBaseWindow 的developer.mozilla.org/en-US/Add-ons/Code_snippets/…,你有任何可以分享的C++ firefox 插件吗? :) 喜欢原始代码并告诉我如何编译它吗? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多