在java中如何检查该字符串以另一个字符串开头
我想得到以另一个在java中如何检查该字符串以另一个字符串开头,java,Java,我想得到以另一个字符串开始的字符串(我应该修复它)。我使用了以下代码,但我也得到了以我不想得到的其他字符串开始的字符串 if (critere.equals("Date")) { String date = jTextField5.getText(); try { FileInputStream file = new FileInputStream(new File
字符串开始的字符串
(我应该修复它)。我使用了以下代码,但我也得到了以我不想得到的其他字符串开始的字符串
if (critere.equals("Date")) {
String date = jTextField5.getText();
try {
FileInputStream file = new FileInputStream(new File(jTextField1.getText()));
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
for (Row row : sheet) {
for (Cell cell : row) {
/* SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d = dateFormat.parse(cell.getRichStringCellValue().getString());
SimpleDateFormat printFormat1 = new SimpleDateFormat("MM/dd/yyyy");
Date dd = printFormat1.parse(printFormat1.format(d));*/
if (cell.getRichStringCellValue().getString().trim().startsWith(date)) {// I have problem here
CellReference ref = new CellReference("A1");
CellReference ref1 = new CellReference("B1");
CellReference ref2 = new CellReference("C1");
CellReference ref3 = new CellReference("D1");
CellReference ref4 = new CellReference("E1");
CellReference ref5 = new CellReference("G1");
CellReference ref6 = new CellReference("I1");
CellReference ref7 = new CellReference("J1");
Cell c = row.getCell(ref.getCol());
Cell c1 = row.getCell(ref1.getCol());
Cell c2 = row.getCell(ref2.getCol());
Cell c3 = row.getCell(ref3.getCol());
Cell c4 = row.getCell(ref4.getCol());
Cell c5 = row.getCell(ref5.getCol());
Cell c6 = row.getCell(ref6.getCol());
Cell c7 = row.getCell(ref7.getCol());
if (c.getStringCellValue().equals("Warning")) {
((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{c.getStringCellValue(), c1.getStringCellValue(), c2.getStringCellValue(),
c3.getStringCellValue(), c4.getStringCellValue(), c5.getStringCellValue(), c6.getStringCellValue(), c7.getStringCellValue()});
}
if (c.getStringCellValue().equals("Minor")) {
((DefaultTableModel) jTable2.getModel()).addRow(new Object[]{c.getStringCellValue(), c1.getStringCellValue(), c2.getStringCellValue(),
c3.getStringCellValue(), c4.getStringCellValue(), c5.getStringCellValue(), c6.getStringCellValue(), c7.getStringCellValue()});
}
if (c.getStringCellValue().equals("Major")) {
((DefaultTableModel) jTable3.getModel()).addRow(new Object[]{c.getStringCellValue(), c1.getStringCellValue(), c2.getStringCellValue(),
c3.getStringCellValue(), c4.getStringCellValue(), c5.getStringCellValue(), c6.getStringCellValue(), c7.getStringCellValue()});
}
if (c.getStringCellValue().equals("Critical")) {
((DefaultTableModel) jTable4.getModel()).addRow(new Object[]{c.getStringCellValue(), c1.getStringCellValue(), c2.getStringCellValue(),
c3.getStringCellValue(), c4.getStringCellValue(), c5.getStringCellValue(), c6.getStringCellValue(), c7.getStringCellValue()});
}
}
}
}
if(标准等于(“日期”)){
字符串日期=jTextField5.getText();
试一试{
FileInputStream file=newfileinputstream(新文件(jTextField1.getText());
XSSF工作簿=新XSSF工作簿(文件);
//从工作簿中获取第一张/所需的工作表
XSSFSheet sheet=workbook.getSheetAt(0);
//逐个遍历每一行
迭代器rowIterator=sheet.Iterator();
用于(行:页){
用于(单元格:行){
/*SimpleDataFormat dateFormat=新的SimpleDataFormat(“MM/dd/yyyy HH:MM:ss”);
Date d=dateFormat.parse(cell.getRichStringCellValue().getString());
SimpleDataFormat printFormat1=新的SimpleDataFormat(“MM/dd/yyyy”);
datedd=printFormat1.parse(printFormat1.format(d))*/
如果(cell.getRichStringCellValue().getString().trim().startsWith(date)){//我这里有问题
CellReference ref=新的CellReference(“A1”);
CellReference ref1=新的CellReference(“B1”);
CellReference ref2=新的CellReference(“C1”);
CellReference ref3=新的CellReference(“D1”);
CellReference ref4=新的CellReference(“E1”);
CellReference ref5=新的CellReference(“G1”);
CellReference ref6=新的CellReference(“I1”);
CellReference ref7=新的CellReference(“J1”);
Cell c=row.getCell(ref.getCol());
单元格c1=row.getCell(ref1.getCol());
单元格c2=行.getCell(ref2.getCol());
单元格c3=row.getCell(ref3.getCol());
Cell c4=row.getCell(ref4.getCol());
单元格c5=row.getCell(参考5.getCol());
Cell c6=row.getCell(ref6.getCol());
Cell c7=row.getCell(ref7.getCol());
如果(c.getStringCellValue().equals(“警告”)){
((DefaultTableModel)jTable1.getModel()).addRow(新对象[]{c.getStringCellValue(),c1.getStringCellValue(),c2.getStringCellValue(),
c3.getStringCellValue()、c4.getStringCellValue()、c5.getStringCellValue()、c6.getStringCellValue()、c7.getStringCellValue();
}
如果(c.getStringCellValue().equals(“Minor”)){
((DefaultTableModel)jTable2.getModel()).addRow(新对象[]{c.getStringCellValue(),c1.getStringCellValue(),c2.getStringCellValue(),
c3.getStringCellValue()、c4.getStringCellValue()、c5.getStringCellValue()、c6.getStringCellValue()、c7.getStringCellValue();
}
如果(c.getStringCellValue().equals(“Major”)){
((DefaultTableModel)jTable3.getModel()).addRow(新对象[]{c.getStringCellValue(),c1.getStringCellValue(),c2.getStringCellValue(),
c3.getStringCellValue()、c4.getStringCellValue()、c5.getStringCellValue()、c6.getStringCellValue()、c7.getStringCellValue();
}
如果(c.getStringCellValue()等于(“临界”)){
((DefaultTableModel)jTable4.getModel()).addRow(新对象[]{c.getStringCellValue(),c1.getStringCellValue(),c2.getStringCellValue(),
c3.getStringCellValue()、c4.getStringCellValue()、c5.getStringCellValue()、c6.getStringCellValue()、c7.getStringCellValue();
}
}
}
}
当日期以我应该在应用程序中输入的日期开始时,我希望进入此循环,但当我运行应用程序时,它会进入此循环,即使日期没有以我输入的值开始参见此示例:
也许您的数据变量不是字符串
public class Test{
public static void main(String args[]){
String str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(str.startsWith("Welcome") );
System.out.print("Return Value :" );
System.out.println(str.startsWith("Tutorials") );
System.out.print("Return Value :" );
System.out.println(str.startsWith("Tutorials", 11) );
}
}
这将产生以下结果:
返回值:true
返回值:false
返回值:true
我假定date
是一个String
变量,因此您需要检查它是否是整体的子字符串,以及它第一次出现的索引是否为0
如果date
不是String
首先将其转换为一个字符串
if(cell.getRichStringCellValue().getString().trim().indexOf(date.toString()) == 0);
我修复了这个问题,我在同一行中有一个以字符串date开头的其他colnum,这就是为什么当我运行代码时,它也会使用另一个colnum中以date开头的字符串。不清楚你在说什么。你能举个例子吗?请展示一个简短但完整的程序来演示这个问题。我们不知道得到了什么String()
返回,或者日期是什么。请检查更新后的postOK。更正..我只是复制它并按上面提到的那样粘贴。:)谢谢!使用。indexOf(date)==0
而不是。startsWith(date)有什么好处