用分隔符拆分 java 中的字符串
SPlit string in java with delimiter
我的字符串是 Spiderman|Batman|Superman
我想拆分字符串,使输出看起来像 Spiderman Batman Superman
我试过这个:
String actors = "Spiderman|Batman|Superman";
String [] temp;
String delimiter = "|";
temp = actors.split(delimiter);
for (int i =0; i<temp.length; i++)
System.out.print(temp[i].replaceAll("\|", " "));
我怎样才能做得更好?输出没有意义。
谢谢你的期待。
输出仍然与输入相同。编译后,它向我显示:
Spiderman|Batman|Superman
好像split方法从来没有启动过。
第二个问题
请看这张图片。如果我使用 100k 字符串,它看起来像这样:
请看我代码中for循环后的注释。我的拆分字符串有效,但我得到了一个奇怪的输出。 (请看截图)我认为这是由 "print" 引起的,或者没有更多的 space 代表近 300 k 个字符串。我将我的数据库项目与 Eclipse 连接起来。在 postgres 中,我导入了一个 .csv 文件,其中包含有关近 30 万部电影的一些信息。如演员导演、年份、评级等。
这是我的问题:
这是我在 Java
中的代码
package dbs;
import java.sql.*;
public class Fertigevariante {
public static void main(String[] args) {
try {
Class.forName("org.postgresql.Driver");
String url= "jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password";
Connection conn = DriverManager.getConnection(url);
Statement st= conn.createStatement();
ResultSet rs= st.executeQuery("SELECT * FROM kinofilme");
while (rs.next()){
String imdib= rs.getString("imdib");
String rating = rs.getString("rating");
int votes = rs.getInt("votes");
String runtime = rs.getString("runtime");
String name = rs.getString("name");
String directors = rs.getString("directors");
String actors = rs.getString("actors");
String [] temp;
String delimiter ="|";
temp = actors.split(delimiter);
for(int j=0 ; j < temp.length; j++)
System.out.print( temp[j].replaceAll("\|", " ")); // here is the output problem, look at the screenshot I uploaded here.
int year = rs.getInt("year");
// System.out.println(imdib + "\t" + name + "\t" + year + "\t" + votes + "\t" + directors + "\t" + actors);
}
rs.close();
st.close();
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
当你拆分然后使用 print
你会得到:"SpidermanBatmanSuperman"
因为你想要一个 space 在中间,你可以将最后一行替换为:
System.out.print(temp[i] + " ");
但是您可以通过简单地替换子字符串“|”来做得更好space: " "
System.out.print("Spiderman|Batman|Superman".replaceAll("\|", " "));
在这两种情况下,输出都是:
Spiderman Batman Superman
重要:
when you split/replace |
you should escape it since it's a special
character that has a regex-meaning!
split
接受正则表达式作为参数 - |
是正则表达式中的特殊字符。尝试 "\|"
作为分隔符,或使用 Pattern.quote(delimiter)
您可以 quote
split
的正则表达式参数
temp = actors.split(Pattern.quote(delimiter));
我的字符串是 Spiderman|Batman|Superman
我想拆分字符串,使输出看起来像 Spiderman Batman Superman
我试过这个:
String actors = "Spiderman|Batman|Superman";
String [] temp;
String delimiter = "|";
temp = actors.split(delimiter);
for (int i =0; i<temp.length; i++)
System.out.print(temp[i].replaceAll("\|", " "));
我怎样才能做得更好?输出没有意义。 谢谢你的期待。 输出仍然与输入相同。编译后,它向我显示:
Spiderman|Batman|Superman
好像split方法从来没有启动过。
第二个问题
请看这张图片。如果我使用 100k 字符串,它看起来像这样:
请看我代码中for循环后的注释。我的拆分字符串有效,但我得到了一个奇怪的输出。 (请看截图)我认为这是由 "print" 引起的,或者没有更多的 space 代表近 300 k 个字符串。我将我的数据库项目与 Eclipse 连接起来。在 postgres 中,我导入了一个 .csv 文件,其中包含有关近 30 万部电影的一些信息。如演员导演、年份、评级等。 这是我的问题:
这是我在 Java
中的代码package dbs;
import java.sql.*;
public class Fertigevariante {
public static void main(String[] args) {
try {
Class.forName("org.postgresql.Driver");
String url= "jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password";
Connection conn = DriverManager.getConnection(url);
Statement st= conn.createStatement();
ResultSet rs= st.executeQuery("SELECT * FROM kinofilme");
while (rs.next()){
String imdib= rs.getString("imdib");
String rating = rs.getString("rating");
int votes = rs.getInt("votes");
String runtime = rs.getString("runtime");
String name = rs.getString("name");
String directors = rs.getString("directors");
String actors = rs.getString("actors");
String [] temp;
String delimiter ="|";
temp = actors.split(delimiter);
for(int j=0 ; j < temp.length; j++)
System.out.print( temp[j].replaceAll("\|", " ")); // here is the output problem, look at the screenshot I uploaded here.
int year = rs.getInt("year");
// System.out.println(imdib + "\t" + name + "\t" + year + "\t" + votes + "\t" + directors + "\t" + actors);
}
rs.close();
st.close();
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
当你拆分然后使用 print
你会得到:"SpidermanBatmanSuperman"
因为你想要一个 space 在中间,你可以将最后一行替换为:
System.out.print(temp[i] + " ");
但是您可以通过简单地替换子字符串“|”来做得更好space: " "
System.out.print("Spiderman|Batman|Superman".replaceAll("\|", " "));
在这两种情况下,输出都是:
Spiderman Batman Superman
重要:
when you split/replace
|
you should escape it since it's a special character that has a regex-meaning!
split
接受正则表达式作为参数 - |
是正则表达式中的特殊字符。尝试 "\|"
作为分隔符,或使用 Pattern.quote(delimiter)
您可以 quote
split
temp = actors.split(Pattern.quote(delimiter));