英文:
Java Business Rule to apply to setter to limit String length and insert space if limit exceeded
问题
我有这个设置方法:
public void setCgp_name(String cgp_name) {
this.cgp_name = cgp_name;
}
基本上,当用户插入像ARG、AUS、BEL、BRA、DNK、FRA、ITA、GBR、USA这样没有在逗号之间加入空格的长字符串时,这些信息会被放入一个表格中,从而突破表格的边界。我正尝试对这个setter应用一条业务规则,以便如果用户输入了这样的内容:ARG、AUS、BEL、BRA、DNK、FRA、ITA、GBR、USA,会在某个地方添加一个空格,以防止表格中的格式问题。当存在空格时,单词换行是有效的,但在长字符串的情况下却不行。
英文:
I have this setter method:
public void setCgp_name(String cgp_name) {
this.cgp_name = cgp_name;
}
Basically when the user inserts a long string such as ARG,AUS,BEL,BRA,DNK,FRA,ITA,GBR,USA without any spaces between the commas this info is placed into a table and it will break the boundaries of the table. I am trying to apply a business rule to this setter so that if the user enters something like this : ARG,AUS,BEL,BRA,DNK,FRA,ITA,GBR,USA a space will be added in somewhere to prevent the formatting issue in the table. The word wrapping works when spaces are present but not with long strings.
答案1
得分: 0
public void setCgp_name(String cgp_name) {
cgp_name = cgp_name.replaceAll(",", ", ");
this.cgp_name = cgp_name;
}
英文:
public void setCgp_name(String cgp_name) {
cgp_name = cgp_name.replaceAll(",[ ]*", ", ");
this.cgp_name = cgp_name;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论