Free Support Forum - aspose.com

How to check space at the beginning of the string?

Hi,
I need to find the space at the beginning of the string.

Code:-


Cells cells = worksheet.getCells();
int num=cells.getMaxDataRow();
int num1=cells.getMaxDataColumn();

FindOptions findOptions = new FindOptions();
findOptions.setLookinType(FindOptions.MATCH_START_WITH);
Cell prevCell = null;
for (int n1=0;n1<=num;n1++)
{
for(int nColIndex =0;nColIndex<=num1;nColIndex++)
{
Cell cell1=cells.getCell(n1,nColIndex);

cell1 = cells.find(" ",prevCell,findOptions);
if(cell1!=null)
{
System.out.println(cell1.getValue());
System.out.println("Name of the cell containing formula: " + cell1.getName());
Style style = cells.getCell( n1,nColIndex).getStyle();
style.setColor(Color.MAGENTA);
cell1.setStyle(style);
prevCell = cell1;
}
}
}

Hi,

Thanks for your posting.

Please see the code below. It searches space in the beginning of the cell’s string. It then outputs the cell name which contains a space in the beginning, then it replaces the Space with the word Rock.

e.g if the string was " Check" and the execution it will become “RockCheck”.

Please see the source and output workbooks.

Java:


String path = “F:\Shak-Data-RW\Downloads\source.xlsx”;


Workbook workbook = new Workbook();

workbook.open(path);


String stringtofind = " ";

String stringtoreplace = “Rock”;


for (int i = 0; i < workbook.getWorksheets().size(); i++)

{

Worksheet worksheet = workbook.getWorksheets().getSheet(i);

Cells cells = worksheet.getCells();

Cell cell;

Cell prevcell = null;


do

{

FindOptions opts = new FindOptions();

opts.setLookinType(FindOptions.MATCH_START_WITH);


cells.find(stringtofind, prevcell, opts);


cell = cells.find(stringtofind, prevcell, opts);


if (cell == null)

break;


System.out.println(cell.getName());


String val = cell.getStringValue().replace(stringtofind, stringtoreplace);


cell.setValue(val);

prevcell = cell;


} while (cell != null);


}

workbook.save(path + “.java.out.xlsx”);

Output:
A1
I17

Thanks mshakeel.faiz,

Given Code finding all the spaces in the Excel Sheet . it is same as the my code.
I need to find only at the beginning of the cell the rest of the spaces should be ignored.

eg.

first cell contains " hello" & second
cell contains “hello World”


then it should be highlighted only the first cell.

Thanks.


Hi,

It appears to me a bug. Anyway, please add an extra check to make sure that string really starts from a space.

Please see the code below

Java


String path = “F:\Shak-Data-RW\Downloads\source.xlsx”;


Workbook workbook = new Workbook();

workbook.open(path);


String stringtofind = " “;

String stringtoreplace = “Rock”;


for (int i = 0; i < workbook.getWorksheets().size(); i++)

{

Worksheet worksheet = workbook.getWorksheets().getSheet(i);

Cells cells = worksheet.getCells();

Cell cell;

Cell prevcell = null;


do

{

FindOptions opts = new FindOptions();

opts.setLookinType(FindOptions.MATCH_START_WITH);


cells.find(stringtofind, prevcell, opts);


cell = cells.find(stringtofind, prevcell, opts);


if (cell == null)

break;



String val = cell.getStringValue();


//Add this check

if(val.startsWith(” "))

{

System.out.println(cell.getName());

val= val.replace(stringtofind, stringtoreplace);

}


cell.setValue(val);

prevcell = cell;


} while (cell != null);


}

workbook.save(path + “.java.out.xlsx”);



Hi,


After further analyzing your code we come to know that it is because of the incorrect option you have set in your code.
To find string starting with space, you should change your line of code:

findOptions.setLookinType(FindOptions.MATCH_START_WITH);

to:

findOptions.setMatchType(FindOptions.MATCH_START_WITH);


I have tested your code and it works fine with the changed line.

Thanks It works Fine