Ternary Operator in String

Skip

I embedded a ternary operator in string as following:

return borderStyle.getThickness() + "|" + 
        borderStyle.getColor()!=null?ColorPersistor.asString(borderStyle.getColor()):"isnull" + "|" + 
                borderStyle.getLineStyle();

Surprisingly ColorPersistor.asString(borderStyle.getColor()) was executed.

Why wasnt the call evaluated to "isnull" Isn't it possible to embed ternary operator in strings?

Jan

The ternary operator has one of the lowest Operator Precedeces.

That means: It is evaluated after the + operations in front of that.

So in fact you're not checking if borderStyle.getColor() is not null but you check that

borderStyle.getThickness() + "|" + borderStyle.getColor() is not null. Which is isn't as the literal String "|" is always != null.

Whenever you use the ternary operator, be sure to use ( and ) to make sure you apply to the correct part of your equation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

String Convert To Ternary Operator

From Dev

String Convert To Ternary Operator

From Dev

Concatenate string with ternary operator in javascript

From Dev

Ternary Operator String Length error

From Java

How to use the ternary operator inside an interpolated string?

From Dev

C++ ternary operator string concatenation

From Dev

TCL ternary operator does not like empty string

From Dev

String to Boolean using ternary operator in Javascript?

From Dev

How can I use a operator ternary in a string?

From Dev

Python string concatenation on series with ternary condition operator

From Dev

Ternary operator adding to dictionary<string, list<uri>>

From Dev

error when using ternary operator in formatting string (aka interpolated string)

From Dev

+ operator in a ternary operator

From Dev

Ruby ternary operator (or) or operator

From Dev

Ternary operator and increment operator

From Dev

Ternary Operator(Elvis Operator) ?:

From Dev

Ternary operator with "OR" operator not working?

From Dev

Ternary operator with string concatination causes partial loss of content?

From Dev

Can I use the conditional ternary operator into a string concatenation?

From Dev

.Net MVC Razor - Concatenating string inside ternary operator

From Dev

Ternary operator with append (<<) operator and match

From Dev

Usage of unary operators applied to ternary operator arguments (eg *operator argument applied to string)

From Dev

Usage of unary operators applied to ternary operator arguments (eg *operator argument applied to string)

From Dev

Simple ternary operator not working

From Dev

Is it recommended to use Ternary operator?

From Dev

Ternary operator in aspx page

From Dev

Multiple parameters with ternary operator

From Java

Kotlin Ternary Conditional Operator

From Dev

Using ternary operator in python?

Related Related

  1. 1

    String Convert To Ternary Operator

  2. 2

    String Convert To Ternary Operator

  3. 3

    Concatenate string with ternary operator in javascript

  4. 4

    Ternary Operator String Length error

  5. 5

    How to use the ternary operator inside an interpolated string?

  6. 6

    C++ ternary operator string concatenation

  7. 7

    TCL ternary operator does not like empty string

  8. 8

    String to Boolean using ternary operator in Javascript?

  9. 9

    How can I use a operator ternary in a string?

  10. 10

    Python string concatenation on series with ternary condition operator

  11. 11

    Ternary operator adding to dictionary<string, list<uri>>

  12. 12

    error when using ternary operator in formatting string (aka interpolated string)

  13. 13

    + operator in a ternary operator

  14. 14

    Ruby ternary operator (or) or operator

  15. 15

    Ternary operator and increment operator

  16. 16

    Ternary Operator(Elvis Operator) ?:

  17. 17

    Ternary operator with "OR" operator not working?

  18. 18

    Ternary operator with string concatination causes partial loss of content?

  19. 19

    Can I use the conditional ternary operator into a string concatenation?

  20. 20

    .Net MVC Razor - Concatenating string inside ternary operator

  21. 21

    Ternary operator with append (<<) operator and match

  22. 22

    Usage of unary operators applied to ternary operator arguments (eg *operator argument applied to string)

  23. 23

    Usage of unary operators applied to ternary operator arguments (eg *operator argument applied to string)

  24. 24

    Simple ternary operator not working

  25. 25

    Is it recommended to use Ternary operator?

  26. 26

    Ternary operator in aspx page

  27. 27

    Multiple parameters with ternary operator

  28. 28

    Kotlin Ternary Conditional Operator

  29. 29

    Using ternary operator in python?

HotTag

Archive