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

String str1 = "test\test\test\test\\";

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

@Test

public void testApp1(){

int specialsymbols = 9;

String str1 = "test\test\test\test\\";

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

}

}

输出结果

00ef9cd246d7dbd1cccfd7ee94999b4f.png

还有其他实现办法吗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值