我在其中有一个具有这种文本类型的css文件:
.fa-glass:before {
content: "\f000";
}
.fa-music:before {
content: "\f001";
}
.fa-search:before {
content: "\f002";
}
我想为文本设置格式,以便返回类似以下内容的内容:
GLASS(f000),
MUSIC(f001),
SEARCH(f0002)
我做了一些研究,发现了很多关于SO的代码,并尝试了以下方法
String result = cssFile.substring(cssFile.indexOf("-") + 1, cssFile.indexOf(":b") +1);
并得到了这个结果:
GLASS:
MUSIC:
SEARCH:
我既不需要名称后的“:”,也不需要空行。我知道为什么它会像这样返回。这是因为在源文件中{}内的文本和方括号本身会扩展为3行。因为当我删除+1时,我得到异常:字符串索引超出范围:-1。java是否可以将第二个大括号和大括号内的文本移动到第一行?我不想手动执行此操作,因为有更多的棕褐色1000个条目。谢谢
以下代码实现了相同的目的。代码不是最佳的,因为我需要着急。请通过使用Stringbuffers和您认为合适的地方进行优化。
File f=new File("test.css");
BufferedReader br=new BufferedReader(new FileReader(f));
String ln;
StringBuffer sb=new StringBuffer();
while((ln=br.readLine())!=null){
sb.append(ln);
}
String[] sections=sb.toString().split("}");
for(String st:sections){
String temp=st.substring(st.indexOf("-")+1, st.indexOf(":")).toUpperCase()+"("+st.substring(st.indexOf("\\")+1, st.indexOf("\";"))+")";
System.out.println(temp);
}
输出 :
玻璃(f000)
音乐(f001)
搜索(f002)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句