Method that concatenates strings, some of which may be null, with several different separators
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I'd like to refactor a Java method I've created, making it cleaner and more beautiful. The method concatenates some strings, which may be empty or not. I'm new on java so i'm not sure how to do that, especially because between the strings I may have separators.
Here is my method:
private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno)
String modeloDescricao = "";
if(modeloVotacao != null)
modeloDescricao = modeloVotacao + " - ";
if(tipoProposicao != null)
modeloDescricao += tipoProposicao + " ";
modeloDescricao += ordemNumero != null?ordemNumero:"";
if(ordemNumero != null && ordemAno != null)
modeloDescricao += "/"+ordemAno;
else
if(ordemAno != null)
modeloDescricao += ordemAno;
return modeloDescricao;
Here is some results that this method returns:
- All filled: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR Nú 16/2018
- modeloVotacao = null: PROJETO DE LEI COMPLEMENTAR Nú 16/2018
- tipoProposicao = null: 1ê DISCUSSÃÂO - Nú 16/2018
- ordemNumero = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR 2018
- ordemAno = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR Nú 16
- all null: empty string
- modeloVotacao and tipoProposicao = null: Nú 16/2018
- ordemNumero and ordemAno = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR
java strings null
add a comment |Â
up vote
2
down vote
favorite
I'd like to refactor a Java method I've created, making it cleaner and more beautiful. The method concatenates some strings, which may be empty or not. I'm new on java so i'm not sure how to do that, especially because between the strings I may have separators.
Here is my method:
private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno)
String modeloDescricao = "";
if(modeloVotacao != null)
modeloDescricao = modeloVotacao + " - ";
if(tipoProposicao != null)
modeloDescricao += tipoProposicao + " ";
modeloDescricao += ordemNumero != null?ordemNumero:"";
if(ordemNumero != null && ordemAno != null)
modeloDescricao += "/"+ordemAno;
else
if(ordemAno != null)
modeloDescricao += ordemAno;
return modeloDescricao;
Here is some results that this method returns:
- All filled: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR Nú 16/2018
- modeloVotacao = null: PROJETO DE LEI COMPLEMENTAR Nú 16/2018
- tipoProposicao = null: 1ê DISCUSSÃÂO - Nú 16/2018
- ordemNumero = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR 2018
- ordemAno = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR Nú 16
- all null: empty string
- modeloVotacao and tipoProposicao = null: Nú 16/2018
- ordemNumero and ordemAno = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR
java strings null
1
Would help a lot if the variable names were in English.
â slowy
Jan 26 at 17:17
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'd like to refactor a Java method I've created, making it cleaner and more beautiful. The method concatenates some strings, which may be empty or not. I'm new on java so i'm not sure how to do that, especially because between the strings I may have separators.
Here is my method:
private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno)
String modeloDescricao = "";
if(modeloVotacao != null)
modeloDescricao = modeloVotacao + " - ";
if(tipoProposicao != null)
modeloDescricao += tipoProposicao + " ";
modeloDescricao += ordemNumero != null?ordemNumero:"";
if(ordemNumero != null && ordemAno != null)
modeloDescricao += "/"+ordemAno;
else
if(ordemAno != null)
modeloDescricao += ordemAno;
return modeloDescricao;
Here is some results that this method returns:
- All filled: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR Nú 16/2018
- modeloVotacao = null: PROJETO DE LEI COMPLEMENTAR Nú 16/2018
- tipoProposicao = null: 1ê DISCUSSÃÂO - Nú 16/2018
- ordemNumero = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR 2018
- ordemAno = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR Nú 16
- all null: empty string
- modeloVotacao and tipoProposicao = null: Nú 16/2018
- ordemNumero and ordemAno = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR
java strings null
I'd like to refactor a Java method I've created, making it cleaner and more beautiful. The method concatenates some strings, which may be empty or not. I'm new on java so i'm not sure how to do that, especially because between the strings I may have separators.
Here is my method:
private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno)
String modeloDescricao = "";
if(modeloVotacao != null)
modeloDescricao = modeloVotacao + " - ";
if(tipoProposicao != null)
modeloDescricao += tipoProposicao + " ";
modeloDescricao += ordemNumero != null?ordemNumero:"";
if(ordemNumero != null && ordemAno != null)
modeloDescricao += "/"+ordemAno;
else
if(ordemAno != null)
modeloDescricao += ordemAno;
return modeloDescricao;
Here is some results that this method returns:
- All filled: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR Nú 16/2018
- modeloVotacao = null: PROJETO DE LEI COMPLEMENTAR Nú 16/2018
- tipoProposicao = null: 1ê DISCUSSÃÂO - Nú 16/2018
- ordemNumero = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR 2018
- ordemAno = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR Nú 16
- all null: empty string
- modeloVotacao and tipoProposicao = null: Nú 16/2018
- ordemNumero and ordemAno = null: 1ê DISCUSSÃÂO - PROJETO DE LEI COMPLEMENTAR
java strings null
edited Jan 27 at 14:38
200_success
123k14143401
123k14143401
asked Jan 26 at 16:57
aseolin
1111
1111
1
Would help a lot if the variable names were in English.
â slowy
Jan 26 at 17:17
add a comment |Â
1
Would help a lot if the variable names were in English.
â slowy
Jan 26 at 17:17
1
1
Would help a lot if the variable names were in English.
â slowy
Jan 26 at 17:17
Would help a lot if the variable names were in English.
â slowy
Jan 26 at 17:17
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
The repeated task here is that you want to conditionally insert a separator between two strings if both are non-null. So, write a helper function that does that:
private static String join(String a, String sep, String b)
return (a == null) ? b :
(b == null) ? a : a + sep + b;
Then, the code becomes simple:
private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno)
String description = join(modeloVotacao, " - ",
join(tipoProposicao, " ",
join(ordemNumero, "/", ordemAno)));
return (description == null) ? "" : description;
Note that in your final example, where ordemNumero
and ordemAno
are both null
, the output ends with a space. That does not happen in my solution; I consider that to be an improvement.
Please avoid mixing Portuguese with English identifiers (such as createString()
). Stick with one or the other (preferably English).
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The repeated task here is that you want to conditionally insert a separator between two strings if both are non-null. So, write a helper function that does that:
private static String join(String a, String sep, String b)
return (a == null) ? b :
(b == null) ? a : a + sep + b;
Then, the code becomes simple:
private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno)
String description = join(modeloVotacao, " - ",
join(tipoProposicao, " ",
join(ordemNumero, "/", ordemAno)));
return (description == null) ? "" : description;
Note that in your final example, where ordemNumero
and ordemAno
are both null
, the output ends with a space. That does not happen in my solution; I consider that to be an improvement.
Please avoid mixing Portuguese with English identifiers (such as createString()
). Stick with one or the other (preferably English).
add a comment |Â
up vote
1
down vote
The repeated task here is that you want to conditionally insert a separator between two strings if both are non-null. So, write a helper function that does that:
private static String join(String a, String sep, String b)
return (a == null) ? b :
(b == null) ? a : a + sep + b;
Then, the code becomes simple:
private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno)
String description = join(modeloVotacao, " - ",
join(tipoProposicao, " ",
join(ordemNumero, "/", ordemAno)));
return (description == null) ? "" : description;
Note that in your final example, where ordemNumero
and ordemAno
are both null
, the output ends with a space. That does not happen in my solution; I consider that to be an improvement.
Please avoid mixing Portuguese with English identifiers (such as createString()
). Stick with one or the other (preferably English).
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The repeated task here is that you want to conditionally insert a separator between two strings if both are non-null. So, write a helper function that does that:
private static String join(String a, String sep, String b)
return (a == null) ? b :
(b == null) ? a : a + sep + b;
Then, the code becomes simple:
private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno)
String description = join(modeloVotacao, " - ",
join(tipoProposicao, " ",
join(ordemNumero, "/", ordemAno)));
return (description == null) ? "" : description;
Note that in your final example, where ordemNumero
and ordemAno
are both null
, the output ends with a space. That does not happen in my solution; I consider that to be an improvement.
Please avoid mixing Portuguese with English identifiers (such as createString()
). Stick with one or the other (preferably English).
The repeated task here is that you want to conditionally insert a separator between two strings if both are non-null. So, write a helper function that does that:
private static String join(String a, String sep, String b)
return (a == null) ? b :
(b == null) ? a : a + sep + b;
Then, the code becomes simple:
private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno)
String description = join(modeloVotacao, " - ",
join(tipoProposicao, " ",
join(ordemNumero, "/", ordemAno)));
return (description == null) ? "" : description;
Note that in your final example, where ordemNumero
and ordemAno
are both null
, the output ends with a space. That does not happen in my solution; I consider that to be an improvement.
Please avoid mixing Portuguese with English identifiers (such as createString()
). Stick with one or the other (preferably English).
edited Jan 27 at 14:40
answered Jan 26 at 19:37
200_success
123k14143401
123k14143401
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186064%2fmethod-that-concatenates-strings-some-of-which-may-be-null-with-several-differ%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Would help a lot if the variable names were in English.
â slowy
Jan 26 at 17:17