Re: [Vala] How to check if string is empty



Le lundi 16 janvier 2017 à 17:27 -0200, Alexandre Oliveira a écrit :
I tried this in my method, even replacing 'string' for 'string?', but
I
still get the same message in my terminal.

What is your suggestion to suppress these messages?

The following snippet works perfectly fine:

    bool is_null_or_empty (string? a) {
            return a == null || a.length == 0;
    }

    assert (is_null_or_empty (null));
    assert (is_null_or_empty (""));
    assert (!is_null_or_empty (" "));


On 16/01/2017 17:23, Guillaume Poirier-Morency wrote:
You should test for nullity before addressing the string.

    return str == null || str.length == 0;

If you expect the string to be potentially 'null', use the
'string?'
type instead.

2017-01-16 14:09 GMT-05:00 Alexandre Oliveira <xinayder airmail cc
<mailto:xinayder airmail cc>>:

    Hello!

    I've just heard about Vala and I've been messing around with it
these
    past days.

    However, I came across a problem. I need to check if some
strings aren't
    empty, and I realized there's no method in Vala that's
equivalent to
    C#'s `string.IsNullOrEmpty(string)`.

    I tried setting up a static method to check if the given string
is
    empty:
    ```
    static bool is_empty(string str) {
        return str[0] == '\0' || str == null;
    }
    ```

    However, running my code, I get the following message
repeatedly in my
    terminal: ** (main.exe:2288): CRITICAL **: is_empty: assertion
'str !=
    NULL' failed

    What am I doing wrong?

    Thanks!
    --
    Alexandre Oliveira
      167F D82F 514A E8D1 2E9E
      C62D 1B63 9D4A 7E9D DA9D
    _______________________________________________
    vala-list mailing list
    vala-list gnome org <mailto:vala-list gnome org>
    https://mail.gnome.org/mailman/listinfo/vala-list
    <https://mail.gnome.org/mailman/listinfo/vala-list>




-- 
Guillaume Poirier-Morency <guillaumepoiriermorency gmail com
<mailto:guillaumepoiriermorency gmail com>>

Étudiant au baccalauréat en Informatique à l'Université de Montréal
Développeur d'application web

*Mon blog:* arteymix.github.io <https://arteymix.github.io>
*Mon projet de coopérative:* pittoresque.github.io
<https://pittoresque.github.io>
*Clé PGP:* B1AD6EA5
<https://pgp.mit.edu/pks/lookup?op=vindex&search=0x1CCFC3A2B1AD6EA5



-- 
Guillaume Poirier-Morency <guillaumepoiriermorency gmail com>

Étudiant au baccalauréat en Informatique à l'Université de Montréal
Chercheur boursier à l'IRIC

Mon blog: https://arteymix.github.io/
Clé PGP: B1AD6EA5

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]