We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to get a substring in word template using Java | LINQ Reporting Engine

I want to get a substring from index 0 to 2. I have used Substring(0,2) but it doesn’t seem to be working. neither Substring(2).
Thanks in advance.

@ter.dt

In template expressions, LINQ Reporting Engine supports using of class members available for a platform you use. In case of Java, you can make use of String.substring as follows:

DocumentBuilder builder = new DocumentBuilder();
builder.write("<<[s.substring(0, 2)]>>");

ReportingEngine engine = new ReportingEngine();
engine.buildReport(builder.getDocument(), "012345", "s");

System.out.println(builder.getDocument().getText().trim()); // Prints "01"
1 Like

Hi,
Thanks for your respose.
It worked, but there is a problem regarding split:

I have used this in word template and it’s not working. Also tried: if[domanda.testo.split(")") , ie with double quotes. The error says unmached end for “)”, so it doesn’t take it as a char or string but as ending tag of the first bracket. I have used split with something else, for example domanda.testo.split(“a”) and it works perfectly fine, so i think the problem is with “)”. i’m using apose 21.11.
Thanks.

@ter.dt

LINQ Reporting Engine properly handles string literals, the problem is an incorrect expression.

In Java, there is no String.split overload accepting a single character, so String.split(')') does not work. Moreover, overloads of String.split accept only regular expressions. Special characters such as ')' should be escaped in regular expressions, so String.split(")") does not work either, but String.split("\\)") works.

Please check the following code:

DocumentBuilder builder = new DocumentBuilder();
builder.write("<<var [a = s.split(\"\\\\)\")]>><<[a[0]]>>, <<[a[1]]>>, <<[a[2]]>>");

System.out.println(builder.getDocument().getText().trim()); // Prints "<<var [a = s.split("\\)")]>><<[a[0]]>>, <<[a[1]]>>, <<[a[2]]>>"

ReportingEngine engine = new ReportingEngine();
engine.buildReport(builder.getDocument(), "1)2)3", "s");

System.out.println(builder.getDocument().getText().trim()); // Prints "1, 2, 3"