How to get the full string from JsVar string?

Posted on
  • I want to get the full string dsafsafsafdsafdsafdsafdsafdsfdsafsafsafd­sfsafsafsafdsaf from JsVar string as follow, but the function jsvAsString() seems to make the string truncated short, any good way to solve the problem? use JsvStringIterator iterator through the JsVar?
    any simple way?

    JsVar *hc = jsvNewFromString('dsafsafsafdsafdsafdsaf­dsafdsfdsafsafsafdsfsafsafsafdsaf');
    JsVar *pStr = jsvAsString(hc);
    jsiConsolePrintf("%s ", hc);
    jsiConsolePrintf("%s ", pStr);
  • Hi! Well, if you just want to print the string/variable, there's a special 'format' character %v that will do it for you (or %q to quote it).

    jsiConsolePrintf("%v ", hc);   // no need even for jsvAsString

    Is that what you're after?

    Otherwise if you need to get every character for use in your own code then JsvStringIterator is more sensible.

    There is another option which is to use the macro JSV_GET_AS_CHAR_ARRAY which will try and get a pointer to the string, will allocate memory on the stack and give you a pointer, or will just fail.

    Ideally don't use that for your own code, but it's useful if you're trying to use some other code which was designed just to work on a flat memory area

  • @Gordon nice, jsiConsolePrintf("%v ", hc); can print the JsVar's full string, but i still can't get the full string for variable defined in c code :( , it seems like the function jsvAsString() only get 20 length

    JsVar *hc = jsvNewFromString('dsafsafsafdsafdsafdsaf­dsafdsfdsafsafsafdsfsafsafsafdsaf');
    char *myStr = jsvAsString(hc);
    while(*myStr ){
    myStr ++;
  • jsvAsString really isn't doing what you expect.

    As I said above, you need JSV_GET_AS_CHAR_ARRAY - take a look at where it's used in other places in the code - it's pretty straightforward

  • it seems like the function jsvAsString() only get 20 length

    maybe it was not clear from previous explanations - the string is not truncated, it is simply not stored in single piece anywhere in memory so to make it as single piece for you it must be created anew - on stack (or elsewhere) taking extra memory. So using iterator is definitely better if you iterate over it anyway, otherwise with larger strings you may overflow the stack

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

How to get the full string from JsVar string?

Posted by Avatar for tyronehell @tyronehell