String error “constant string too long”

前端 未结 4 1024
隐瞒了意图╮
隐瞒了意图╮ 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:44

    Strings Longer than 64k are not allowed to be used directly in java, but you can use them indirectly.

    • Step 1) click on your string
    • Step 2) press Alt + Enter keys together
    • Step 3) Select Extract String resource
    • Step 4) Name the resource and hit enter key.

    That is it. It will generate a string for you in strings.xml If you already have string in your strings.xml you can use this code to retrieve it:

    String yourStr = getString(R.string.sampleBigString);
    

提交回复
热议问题