Java业务规则应用于setter,以限制字符串长度,并在超出限制时插入空格。

huangapple go评论68阅读模式
英文:

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;
    }

huangapple
  • 本文由 发表于 2020年4月3日 21:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61012489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定