【开发笔记】- 输出String字符串使其文本对齐
需求
- 一段文本做每64个字节换行处理,并添加对应的头尾注释
代码实现
public static String certFormat(String code, String beginTitle, String endTitle){ StringBuffer sb = new StringBuffer(cert); for (int i = 0; i < cert.length(); i++) { char c = cert.charAt(i); if(i%64 ==0 && i != 0){ sb.insert(i,"\n\t"); } } sb.insert(0, beginTitle+"\n\t"); sb.insert(sb.length(), "\n\t"+endTitle); return sb.toString(); }
输出文本
-----BEGIN CERTIFICATE REQUEST----- MIIBQjCB6QIBADCBhjELMAkGA1UEBhMCQ04xEDAOBgNVBAgMB0JlaUppbmcxEDAO BgNVBAcMB0JlaUppbmcxEDAOBgNVBAoMB0luZm9TZWMxDDAKBgNVBAsMA1ImRD EPMA0GA1UEAwwGc2VydmVyMSIwIAYJKoZIhvcNAQkBFhNsaWprQGluZm9zZWMu Y29tLmNuMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEvpAS1JSWB+ywk4g/1T r/iVBSfQOmVHwxKHBVBaCqjb5m9fsNYOE+X2gPy6OyV6WRI7G80CCQIRruMne4 +NC66qAAMAoGCCqBHM9VAYN1A0gAMEUCIQCnfYW9672h7WO7BcOom8cNJp16a1 t5M8hLQTcPhktQMQIgZ8RM9L4iTDkNwvIzwvD4K18hhJ6nZEdgI4rviEv5fpc= -----END CERTIFICATE REQUEST-----