找到子字符串时,我需要帮助才能分割字符串,仅在最近一次出现时
Substring to search(example):123
String: 123hello123boy123guy123girl
Res: 123hello123boy123guy (result on split[0])
Ex: hello123boy
Res:hello
我正在尝试使用此功能,但这只能在第一个资源上进行分割。
public static String[] getSplitArray(String toSplitString, String spltiChar) {
return toSplitString.split("(?<=" + spltiChar + ")");
}
答案 0 :(得分:4)
如果您想在单split()
中执行此操作,则应该可以:
string.split("123((?!123).)*?$")
E.g:
String s = "foo123bar123hhh123tillHere123Bang";
System.out.println(s.split("123((?!123).)*?$")[0]);
输出:
foo123bar123hhh123tillHere
另一种方法是split("123")
,然后join
123
所需的元素作为分隔符。
答案 1 :(得分:0)
您不需要正则表达式,只需使用String#lastIndexOf
:
public class ContactDAO {
public List<Contact> findAll() {
List<Contact> list = new ArrayList<Contact>();
Connection c = null;
String sql = "SELECT * FROM KIT.CONTACT";
try {
c = DBConnection.getConnection();
Statement s = c.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
list.add(processRow(rs));
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
DBConnection.close(c);
}
return list;
}
public List<Contact> findByCity(String city) {
List<Contact> list = new ArrayList<Contact>();
Connection c = null;
String sql = "SELECT * FROM KIT.CONTACT as e " + "WHERE UPPER(city) LIKE ? ";
try {
c = DBConnection.getConnection();
PreparedStatement ps = c.prepareStatement(sql);
ps.setString(1, "%" + city.toUpperCase() + "%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
list.add(processRow(rs));
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
DBConnection.close(c);
}
return list;
}
public Contact findById(int id) {
String sql = "SELECT * FROM KIT.CONTACT WHERE id = ?";
Contact contact = null;
Connection c = null;
try {
c = DBConnection.getConnection();
PreparedStatement ps = c.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
contact = processRow(rs);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
DBConnection.close(c);
}
return contact;
}
public Contact save(Contact contact) {
return contact.getId() > 0 ? update(contact) : insert(contact);
}
public Contact insert(Contact contact) {
Connection c = null;
PreparedStatement ps = null;
try {
c = DBConnection.getConnection();
ps = c.prepareStatement(
"INSERT INTO KIT.CONTACT (country, city, address, photo,fk_user) VALUES (?, ?, ?, ?, ?)",
new String[] { "ID" });
ps.setString(1, contact.getCountry());
ps.setString(2, contact.getCity());
ps.setString(3, contact.getAddress());
ps.setString(4, contact.getPhoto());
ps.setInt(5, contact.getFk_user());
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
while(rs.next()){
int id = rs.getInt(1);
contact.setId(id);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
DBConnection.close(c);
}
return contact;
}
public Contact update(Contact contact) {
Connection c = null;
try {
c = DBConnection.getConnection();
PreparedStatement ps = c
.prepareStatement("UPDATE KIT.CONTACT SET country=?, city=?, address=?, photo=? WHERE id=?");
ps.setString(1, contact.getCountry());
ps.setString(2, contact.getCity());
ps.setString(3, contact.getAddress());
ps.setString(4, contact.getPhoto());
ps.setInt(5, contact.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("contactDAO update exception");
throw new RuntimeException(e);
} finally {
DBConnection.close(c);
}
return contact;
}
public boolean remove(int id) {
Connection c = null;
try {
c = DBConnection.getConnection();
PreparedStatement ps = c
.prepareStatement("DELETE FROM KIT.CONTACT WHERE id=?");
ps.setInt(1, id);
int count = ps.executeUpdate();
return count == 1;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
DBConnection.close(c);
}
}
protected Contact processRow(ResultSet rs) throws SQLException {
Contact contact = new Contact();
contact.setId(rs.getInt("id"));
contact.setCountry(rs.getString("country"));
contact.setCity(rs.getString("city"));
contact.setAddress(rs.getString("address"));
contact.setPhoto(rs.getString("photo"));
return contact;
}
}
答案 2 :(得分:0)
Kent答案的改进:这实际上会为您提供一个拆分数组而不是一个包含String
第一部分的单元素数组:
String[] text = {
"123hello123boy123guy123girl", // 123hello123boy123guy
"hello123boy"// hello
};
for (String s: text) {
System.out.println(
Arrays.toString(
s.split("123(?=((?!123).)*?$)")
)
);
}
<强>输出强>
[123hello123boy123guy, girl]
[hello, boy]