向字符串数组中添加多个字符串值
问题描述:
我有一个程序,其中我调用读取xls文件并获取第2列的方法。向字符串数组中添加多个字符串值
public static String readExcel(String FileName) throws BiffException,
IOException, java.text.ParseException, InterruptedException {
String ss = null;
FileInputStream fs = new FileInputStream(FileName);
HSSFWorkbook workbook = null;
workbook = new HSSFWorkbook(fs);
// TO get the access to the sheet
HSSFSheet sh = workbook.getSheet("data");
int rowCount = sh.getLastRowNum() - sh.getFirstRowNum();
DataFormatter formatter = new DataFormatter(Locale.US);
for (int i = 0; i < rowCount; i++) {
Row row = sh.getRow(i);
// Create a loop to print cell values in a row
for (int j = 1; j < 2; j++) {
Cell alpha = row.getCell(j);
String Beta = formatter.formatCellValue(alpha);
String leftRemoved = Beta.substring(0);
String GammaRemove = leftRemoved.trim();
dateStringList.add(GammaRemove);
// System.out.println(dateStringList);
}
}
System.out.println("The String contains" + dateStringList);
ArrayList<String> Beta = dateStringList;
String[] s1 = new String[256];
for (int k = 1; k < dateStringList.size(); k++) {
String gamma = Beta.get(k);
StringBuilder sb = new StringBuilder(gamma);
sb.deleteCharAt(0);
System.out.println("The New String is" + sb);
// String ss1=sb.toString();
// System.out.println("The String SS is" + ss);
ss = sb.toString();
System.out.println("The String SS is" + ss);
}
fs.close();
return ss;
}
我想使用stringBuilder删除第二列字符串值的前导空间。但我想从这个方法中获取所有这些stringbuilder的值,并将它们存储在字符串数组中。我无法做到这一点,因为它是抛出类型不匹配的错误。
请建议我如何从此StringBuilder获取所有字符串值并将其存储在字符串数组中。
谢谢
答
你不需要为它使用StringBuilder。您可以直接对字符串使用trim()函数。
String gamma = Beta.get(k);
gamma=gamma.trim();
然后您可以直接将它们保存在数组中。
+0
感谢您的重播。 :) –
+0
欢迎@shahabazahmed,如果这解决了你的问题,请接受答案。 –
如果你只是想删除前导/尾随空格,你可以调用一个字符串的'.trim()'方法 – Gikkman
你的代码中的哪个位置出现错误?如果你只是删除String中的第一个字符,你可以使用substring方法(s.substring,1); – pringi