有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

C++字符串长度不同于java字符串长度。JNI安卓

我正在使用一个项目,它使用JNI来集成一个Android项目中的C++后台LIBS。p>

我正在使用Spababl Stand类,并将CuffScript()函数直接从C++调用到样式,而不必在前端爪哇重新运行代码。p>

我有一个字符串,它有如下标记(比如html):

"This is a test {is} italics {ie} that we are using to demonstrate app functionality..." obviously with more text.

< >我在C++中循环遍历所有字符串,并保存它们的位置,然后将斜体跨度添加到我的SPANABABLE字符串中。我的循环是这样的:

        int its = 0, fns = 0, crfs = 0;
    for(int i = 0; i < buff.size(); ++i){

        if(buff[i] != '{')
            continue;

        string tmp = "";

        for(int a = i + 1; a < i + 5; ++a){
            if(buff[a] == '}')
                break;
            tmp += buff[a];
        }

        if(tmp == "is"){
            its = i;
            //start of italics.
        } else if(tmp == "ie"){
            //end of italic tag.
            if(its == 0)
                continue;

            set_spannable(spannable, new_style_obj(italic), its + 4, i);
            its = 0;
        } else if(tmp == "fn"){
            //new footnote tag.
        } else if(tmp == "cf"){
            //new cross reference tag.
        }

    }

代码可以完美地编译和运行,但斜体的位置与java字符串中的位置没有直接关联。出于某种原因,它一直在增加,直到我的斜体字不在它们应该在的地方

我在java中运行了相同的循环,它运行得非常完美:

        String buff = sp.toString();
    int its = 0;
    for(int i = 0; i < buff.length(); ++i){

        if(buff.charAt(i) != '{')
            continue;

        String tmp = "";

        for(int a = i + 1; a < i + 5; ++a){
            if(buff.charAt(a) == '}')
                break;
            tmp += buff.charAt(a);
        }

        System.out.println(tmp);

        if(tmp.equals("is")){
            its = i;
        } else if(tmp.equals("ie")){
            if(its == 0)
                continue;
            System.out.println("Span from " + its + " to " + i);// + //buff.substr(its + 4, (i-4) -its) + "))";
            sp.setSpan(new StyleSpan(1), its + 4, i, 1);


            // set_spannable(spannable, new_style_obj(italic), its, i);
            its = 0;
        }

    }
    tv.setText(sp);

有趣的是,java中的字符串长度总是大于C++中的字符串长度。

我已经使用strlen(string.c_str())和string对它进行了测试。大小();这两个函数都不会返回与java调用字符串相同的长度。长度

有人知道是什么导致了这种差异,以及如何解决它吗?有没有用C++读取java字符的字符?

提前感谢您的帮助

更新1:以下是标签位置数据>>

C++
    Span from 26 to 36
     Span from 146 to 152
     Span from 466 to 473
     Span from 1438 to 1445
     Span from 1726 to 1733
     Span from 1913 to 1920
     Span from 2157 to 2167
     Span from 2228 to 2239
     Span from 2289 to 2299
     Span from 2544 to 2555
     Span from 2827 to 2834
     Span from 2965 to 2972
     Span from 3293 to 3300
     Span from 3913 to 3920
     Span from 4016 to 4023
     Span from 4378 to 4385
     Span from 4467 to 4474
     Span from 5179 to 5195
     Span from 5337 to 5344

Java
    Span from 26 to 36
    Span from 146 to 152
    Span from 462 to 469
    Span from 1426 to 1433
    Span from 1710 to 1717
    Span from 1897 to 1904
    Span from 2139 to 2149
    Span from 2208 to 2219
    Span from 2269 to 2279
    Span from 2520 to 2531
    Span from 2803 to 2810
    Span from 2939 to 2946
    Span from 3265 to 3272
    Span from 3877 to 3884
    Span from 3980 to 3987
    Span from 4340 to 4347
    Span from 4427 to 4434
    Span from 5129 to 5145
    Span from 5285 to 5292

共 (1) 个答案

  1. # 1 楼答案

    更新的表与假设字符串一致,包含一个以上的字符,在C++中,用一个以上的字符表示。p>

    这个问题没有简单的解决办法。在计数时,minimalistic approach将跳过所有具有utf8连续位的字节(即10的前两位),即0x800xbf之间的字节

    或者,您可以使用两字节的UCS-16字符串,这是我们从JNI函数GetStringChars()获得的,它精确地再现了Java表示(即,在Java和C++中长度相同),但库支持较差(没有函数或std::string提供帮助)

    或者您可以使用codecvt_utf8()将utf8字符串转换为wchar__t