String error “constant string too long”

前端 未结 4 1023
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 06:29

There is a 100,000-character text that need to be displayed. If I put it into String object, I get an error \"constant string too long\". The same is with StringBuffer object.

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-08 06:58

    I had same problem and the solution was the very big string literal that was assigned to one constant, to split it to several smaller literals assigned to new constants and concatenate them.

    Example:

    Very big string that can not compile:

    private static String myTooBigText = "...";
    

    Split it to several constants and concatenate them that compiles:

    private static String mySmallText_1 = "...";
    private static String mySmallText_2 = "...";
    ...
    private static String mySmallText_n = "...";
    
    private static String myTooBigText = mySmallText_1 + mySmallText_2 + ... + mySmallText_n;
    

提交回复
热议问题