如何在 Java 中从三行中提取一行?
How can I make one row out of three rows in Java?
我在 :
中有一个包含此类文本的 css 文件
.fa-glass:before {
content: "\f000";
}
.fa-music:before {
content: "\f001";
}
.fa-search:before {
content: "\f002";
}
我希望对文本进行格式化,使其 return 类似于 :
GLASS(f000),
MUSIC(f001),
SEARCH(f0002)
我做了一些研究,发现了很多关于 SO 的代码并尝试了以下方法
String result = cssFile.substring(cssFile.indexOf("-") + 1, cssFile.indexOf(":b") +1);
得到这个结果:
GLASS:
MUSIC:
SEARCH:
我既不想要名字后面的“:”,也不想要空行。我知道为什么 return 会这样。这是因为在源文件中,{} 内的文本和括号本身扩展了 3 行。因为当我删除 +1 时出现异常:字符串索引超出范围:-1。
java 有没有办法将第二个大括号和大括号内的文本移动到第一行?我不想手动完成,因为有更多 tan 1000 条目。
谢谢
如果你有 notepad++(或任何可以 "replace all" 基于模式的文本编辑器 - 我已经使用 notepad++ 并为此选择了 'Extended' 搜索模式 - 你可以做出漂亮的改变快。
第一次,找到{\r\n,替换为空字符串。
第二次,找到\r\n},替换为空字符串。
最终结果是
.fa-glass:before content: "\f000";
.fa-music:before content: "\f001";
.fa-search:before content: "\f002";
如果要保留大括号,只需在替换过程中更改 'replace with' 部分即可
问题是当该行既不包含字符“-”也不包含字符“:”时,您的表达式不匹配。
这就是你的空行的情况:
- 行
.fa-glass:before {
:包含“:”和“-”,因此它产生:glass:
- 第
content: "\f000";
行不包含“:”,因此您的代码生成一个空字符串 (cssFile.substring(0, 0)=""
)
以此类推
我已经编写了一个简单的测试来满足您的需求:
@Test
public void testConcat() throws Exception {
/*
* String that simulates a file with this content:
* .fa-glass:before {
* content: "\f000";
* }
* .fa-music:before {
* content: "\f001";
* }
* .fa-search:before {
* content: "\f002";
* }
*/
String cssFile = ".fa-glass:before {\n content: \"\f000\";\n}\n.fa-music:before {\n content: \"\f001\";\n}\n.fa-search:before {\ncontent: \"\f002\";}";
BufferedReader br = new BufferedReader(new StringReader(cssFile));
String line = null;
String name = null;
String value = null;
StringBuilder result = new StringBuilder();
while( (line = br.readLine()) != null) {
if(line.contains(".fa-")) {
name = line.substring(line.indexOf(".fa-") + 4, line.indexOf(":")).toUpperCase();
} else if(line.contains(";")){
value = line.substring(line.indexOf(": \"") + 3, line.lastIndexOf("\""));
result.append(String.format("%s(%s)\n", name, value));
}
}
/*
* It will print:
* GLASS(\f000)
* MUSIC(\f001)
* SEARCH(\f002)
*/
System.out.println(result.toString());
assertThat(result.toString()).isEqualTo("GLASS(\f000)\nMUSIC(\f001)\nSEARCH(\f002)\n");
}
下面的代码实现了同样的效果。代码不是最佳的,因为我需要赶时间。请通过使用 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)
我在 :
中有一个包含此类文本的 css 文件.fa-glass:before {
content: "\f000";
}
.fa-music:before {
content: "\f001";
}
.fa-search:before {
content: "\f002";
}
我希望对文本进行格式化,使其 return 类似于 :
GLASS(f000),
MUSIC(f001),
SEARCH(f0002)
我做了一些研究,发现了很多关于 SO 的代码并尝试了以下方法
String result = cssFile.substring(cssFile.indexOf("-") + 1, cssFile.indexOf(":b") +1);
得到这个结果:
GLASS:
MUSIC:
SEARCH:
我既不想要名字后面的“:”,也不想要空行。我知道为什么 return 会这样。这是因为在源文件中,{} 内的文本和括号本身扩展了 3 行。因为当我删除 +1 时出现异常:字符串索引超出范围:-1。 java 有没有办法将第二个大括号和大括号内的文本移动到第一行?我不想手动完成,因为有更多 tan 1000 条目。 谢谢
如果你有 notepad++(或任何可以 "replace all" 基于模式的文本编辑器 - 我已经使用 notepad++ 并为此选择了 'Extended' 搜索模式 - 你可以做出漂亮的改变快。
第一次,找到{\r\n,替换为空字符串。 第二次,找到\r\n},替换为空字符串。
最终结果是
.fa-glass:before content: "\f000";
.fa-music:before content: "\f001";
.fa-search:before content: "\f002";
如果要保留大括号,只需在替换过程中更改 'replace with' 部分即可
问题是当该行既不包含字符“-”也不包含字符“:”时,您的表达式不匹配。
这就是你的空行的情况:
- 行
.fa-glass:before {
:包含“:”和“-”,因此它产生:glass:
- 第
content: "\f000";
行不包含“:”,因此您的代码生成一个空字符串 (cssFile.substring(0, 0)=""
)
以此类推
我已经编写了一个简单的测试来满足您的需求:
@Test
public void testConcat() throws Exception {
/*
* String that simulates a file with this content:
* .fa-glass:before {
* content: "\f000";
* }
* .fa-music:before {
* content: "\f001";
* }
* .fa-search:before {
* content: "\f002";
* }
*/
String cssFile = ".fa-glass:before {\n content: \"\f000\";\n}\n.fa-music:before {\n content: \"\f001\";\n}\n.fa-search:before {\ncontent: \"\f002\";}";
BufferedReader br = new BufferedReader(new StringReader(cssFile));
String line = null;
String name = null;
String value = null;
StringBuilder result = new StringBuilder();
while( (line = br.readLine()) != null) {
if(line.contains(".fa-")) {
name = line.substring(line.indexOf(".fa-") + 4, line.indexOf(":")).toUpperCase();
} else if(line.contains(";")){
value = line.substring(line.indexOf(": \"") + 3, line.lastIndexOf("\""));
result.append(String.format("%s(%s)\n", name, value));
}
}
/*
* It will print:
* GLASS(\f000)
* MUSIC(\f001)
* SEARCH(\f002)
*/
System.out.println(result.toString());
assertThat(result.toString()).isEqualTo("GLASS(\f000)\nMUSIC(\f001)\nSEARCH(\f002)\n");
}
下面的代码实现了同样的效果。代码不是最佳的,因为我需要赶时间。请通过使用 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)