`
wayfarer
  • 浏览: 294711 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

回车换行符

阅读更多

11

换行符:另起一行,'\n'10,仅仅把一个长行截断成两行,但还属于同一个段落
回车符:回到一行的开头  '\r'13,分成两个段落

Windows:回车换行符号是"\r\n"
Linux等Unix:只有"\n"没有"\r"
Mac:只有"\r"没有"\n"
在解析文本或其他格式的文件内容时,常常要碰到判定回车换行的地方,这个时候就要注意既要判定"\r\n"又要判定"\n"。

 

public static void main(String[] args) {
	String text="今天是个\n好 \r日子";  
	char[] ch=text.toCharArray();  
	for(int i=0;i<text.length();i++){  
		if((byte)ch[i]==32){  
			ch[i]='A';  
		}else if((byte)ch[i]==10){  
			ch[i]='B';  
		}else if((byte)ch[i]==13){  
			ch[i]='C';  
		} 
	}  
	text=String.valueOf(ch);  
	System.out.println(text);  
  
	text="明天是个\n坏 \r日子";  
	ch=text.toCharArray();  
	for(int i=0;i<text.length();i++){  
		if(ch[i]==32){  
			ch[i]='A';  
		}else if(ch[i]=='\n'){  
			ch[i]='B';  
		}else if(ch[i]=='\r'){  
			ch[i]='C';  
		}
	}  
	text=String.valueOf(ch);  
	System.out.println(text);  
  
//		text="后天是个\n烂 \r日子";  
	text="后天是个\r\n烂 \n\r日子"; 
	for(int i=0;i<text.length() - 2;i++){  
		if(ch[i]==32){  
			ch[i]='A';  
		}else if("\r\n".equals(text.substring(i, i+2))){  
			System.out.println("There is a 111");
		}else if("\n\r".equals(text.substring(i, i+2))){  
			System.out.println("There is a 222");
		}
	}  
	System.out.println(text);  
}
 

 

22

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics