java 字符串原样输出_java原样输出特殊符号

String str1 = "test est est est\";

这个语句在java里面是不好原样输出,应为“”作为转义符号使“ ”有了另外的含义。通过操纵assic 码可以实现原样输出

@Test

public void testApp1(){

int specialsymbols = 9;

String str1 = "test est est est\";

System.out.println(str1);

byte[] bytes = str1.getBytes();

List indexs = new ArrayList();

List list = new LinkedList();

for(int i=0 ;i

if(bytes[i]==specialsymbols){

indexs.add(i);

}

list.add(bytes[i]);

}

int indexpos = 0;

int increate = 0;

for(Integer index : indexs) {

indexpos = index+increate;

list.add(indexpos, (byte)92);

list.add(indexpos+1, (byte)116);

increate += 2;

}

byte[] newbyte = new byte[list.size()];

int i=0;

for (Iterator iterator = list.iterator(); iterator.hasNext();) {

Byte byte1 = (Byte) iterator.next();

if(byte1 == specialsymbols){

iterator.remove();

} else {

newbyte[i]=byte1;

i++;

}

}

输出结果

还有其他实现办法吗

String str1 = "test est est est\"; 这个语句在java里面是不好原样输出,应为“”作为转义符号使“ ”有了另外的含义。通过操纵assic 码可以实现原样输出 @Test public void testApp1(){ int specialsymbols = 9; String str1 = "test est est est\"; System.out.println(str1); byte[] bytes = str1.getBytes(); List indexs = new ArrayList(); List list = new LinkedList(); for(int i=0 ;i if(bytes[i]==specialsymbols){ indexs.add(i); } list.add(bytes[i]); } int indexpos = 0; int increate = 0; for(Integer index : indexs) { indexpos = index+increate; list.add(indexpos, (byte)92); list.add(indexpos+1, (byte)116); increate += 2; } byte[] newbyte = new byte[list.size()]; int i=0; for (Iterator iterator = list.iterator(); iterator.hasNext();) { Byte byte1 = (Byte) iterator.next(); if(byte1 == specialsymbols){ iterator.remove(); } else { newbyte[i]=byte1; i++; } } 输出结果 还有其他实现办法吗
经验分享 程序员 微信小程序 职场和发展