java使用split切割字符串的时候,注意转义字符
今天在做项目的时候发现一个奇怪的问题
File file = new File("d:\\a.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String text = "";
while ((text = br.readLine()) != null) {
String[] s = text.split("|");
for (int i = 0; i < s.length; i++) {
System.out.print("切割字符串" + s[i] + "\t");
}
System.out.println();
}
br.close();
运行的结果
发现每一个字符都给我切割了,后来在网上查到,当以 | 切割的时候一定要注意使用转义字符
File file = new File("d:\\a.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String text = "";
while ((text = br.readLine()) != null) {
String[] s = text.split("\\|");
for (int i = 0; i < s.length; i++) {
System.out.print("切割字符串" + s[i] + "\t");
}
System.out.println();
}
br.close();
搞定收工~
java使用split切割字符串的时候,注意转义字符的更多相关文章
- golang学习笔记15 golang用strings.Split切割字符串
golang用strings.Split切割字符串 kv := strings.Split(authString, " ") if len(kv) != 2 || kv[0] != ...
- java关于split分割字符串,空的字符串不能得到的问题
java关于split分割字符串,空的字符串不能得到的问题 class T { public static void main(String args[]) { String num[] = ne ...
- react FileReader读取TXT文件并保存 split切割字符串 map()分别渲染切割后的数组内的所有字符串
//class my_fileReader( e ) { console.log(e.target.files[0]); const reader = new File ...
- java按照字节切割字符串,解决汉字的问题
编写一个截取字符串的函数,输入为一个字符串,截取开始地址,截取字节数,输出为按字节截取的字符串. 但是要保证汉字不被截半个, 如“我ABC”,0,4,应该截为“我AB”,输入“我ABC汉DEF”,1, ...
- sql server split切割字符串
create FUNCTION [dbo].[dnt_split] ( @splitstring varchar(max), @separator CHAR() = ',' ) RETURNS @sp ...
- split+ Pattern切割字符串
今天在对一个String对象进行拆分的时候,总是无法到达预计的结果.呈现数据的时候出现异常,后来debug之后才发现,错误出在String spilt上,于是开始好好研究下这东西,开始对api里的sp ...
- split切割.号的字符串
excel中的日期为下图所示,利用io读取到后,调试发现值为“12.10.2019”,需要将其转换为“2019-10-12” 用split方法以.号切割时,需要用转移字符“\\.”,代码如下 pack ...
- String常用使用方法,1.创建string的常用3+1种方式,2.引用类型使用==比较地址值,3.String当中获取相关的常用方法,4.字符串的截取方法,5.String转换常用方法,6.切割字符串----java
一个知识点使用一个代码块方便查看 1.创建string的常用3+1种方式 /* 创建string的常用3+1种方式 三种构造方法 public String():创建一个空字符串,不含有任何内容: p ...
- Java OOP中的字符串篇
字符串的三大特征: String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 一.定义 查看 API 会发现,String.St ...
随机推荐
- 【iCore3 双核心板_ uC/OS-III】例程八:互斥信号量
实验指导书及代码包下载: http://pan.baidu.com/s/1geDzqqn iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- C#的TreeView标记
今天用到了TreeView控件,多次添加后发现内容是重复的,于是用到清除:this.myTreeView.Nodes.Clear(): 如果想在添加完节点后,默认全展开:this.myTreeView ...
- ConcurrentHashMap Put()操作示例代码
非常简练: private static void put(MetricKey key, float value) { MetricValue current; do { current = map. ...
- Windows上搭建Kafka运行环境
完整解决方案请参考: Setting Up and Running Apache Kafka on Windows OS 在环境搭建过程中遇到两个问题,在这里先列出来,以方便查询: 1. \Jav ...
- Spring mvc 下Ajax获取JSON对象问题 406错误
我在学习springmvc过程中(我的项目是配置的后缀是.html),从controller返回对象. 如果我不使用 mvc-annotation-driver,而是手动配置,AnnotationMe ...
- java中nextLine()和next()的区别
>概述 在实现字符窗口的输入时,我个人更喜欢选择使用扫描器Scanner,它操作起来比较简单.我发现用Scanner实现字符串的输入有两种方法,一种是next(),一种nextLine(),但是 ...
- EBS R12.2 创建应用层的启动和关闭脚本
Create the following files to start and stop R12. application tier. Change the apps and weblogic pas ...
- react input 获取/失去焦点
<div className={ this.state.focus ? "dis_bottom_left_onfocus" : "dis_bottom_left&q ...
- matlab中动态绘图并保存为视频的小例子
如题,多的就不说了,先上一个效果: 每隔0.1秒,绿色的直线转动一个角度. 再看看代码如何实现: fuction main clear; clc; %%%%%%%%%%%%%%%%%%%%%%%%%% ...
- zjuoj 3602 Count the Trees
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602 Count the Trees Time Limit: 2 Seco ...