> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/variables-and-expressions/expressions-java-methods.md).

# Use Java string methods

You can use almost any [Java string method](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#method.summary) to extract characters or manipulate [expression](/harness-ai/use-harness-platform/variables-and-expressions/harness-variables.md) strings.

### Format <a href="#format" id="format"></a>

The correct format for using a Java method with an expression is to wrap both the source expression and the method in the expression delimiter (`<+...>`), resulting in nested delimiters. For example:

```
<+<+expression>.methodName()>
```

The `<+expression>` calls the variable that you want to use the method on. Then you append the method and any parameters after the expression. The entire statement is wrapped in an expression delimiter (`<+...>`) so that Harness knows to evaluate the entire statement as one expression returning one, resolved value.

The content between the expression delimiter (`<+...>`) is passed to [JEXL](http://commons.apache.org/proper/commons-jexl/) where it is evaluated. Using JEXL, you can build complex variable expressions that use JEXL methods. For example, here is an expression that uses Webhook Trigger payload information:

```
<+<+trigger.payload.pull_request.diff_url>.contains("triggerNgDemo")> || <+trigger.payload.repository.owner.name> == "harness-software"
```

{% hint style="info" %}
**METHOD ORDER RESOLUTION**

Users may experience unexpected behavior when attempting to resolve a method on a variable that is not yet resolved.

For example: `<+<+infra.name>.replace("-", "_")>`

If `<+infra.name>` is **unresolved at the time of execution** but becomes available later in the pipeline, the `replace` method may be applied to a non-existent value. This can result in the expression resolving to just `<+infra.name>` rather than executing the full `replace()` logic.

To avoid this, use the following format instead: `<+infra.name.replace("-", "_")>` This ensures that the `replace` method is applied **after** the variable is properly resolved.
{% endhint %}

#### Escaping <a href="#escaping" id="escaping"></a>

If the method takes multiple, comma-separated arguments, and the method expects the parameters to be strings, you must wrap the expression in double quotes, except if it is a `<+secrets.getValue()>` expression.

Here is an example where an expression supplied as an argument for the `replace()` method. Because the parameter must be a string, the expression is wrapped in quotation marks.

```
<+<+pipeline.variables.var2>.replace("a", "<+pipeline.variables.var1>")>
```

{% hint style="warning" %}
This expression will not work if either `<+pipeline.variables.var1>` or `<+pipeline.variables.var2>` remains unresolved during execution.
{% endhint %}

### Java string method examples <a href="#java-string-method-examples" id="java-string-method-examples"></a>

Here are some examples of Java string methods with expressions.

#### contains() <a href="#contains" id="contains"></a>

Use `contains()` to check if a value contains a certain string. Make sure to wrap the expression and the method within the expressions delimiter and wrap the target string in double quotes, such as `<+<+EXPRESSION.TO.SEARCH>.contains("TARGET STRING")>`.

For example, this expression uses `contains()` to check if a trigger payload field contains the string `triggerDemo`:

```
<+<+trigger.payload.pull_request.diff_url>.contains("triggerDemo")>
```

As another example, assume you have a variable `deployEnv` and you want to check if it contains the string `prod`. You can use `contains()` like this:

```
<+<+pipeline.variables.deployEnv>.contains("prod")>
```

You can also nest an expression in the `contains()` method to check if one expression contains the entire value of another expression, for example:

```
<+<+pipeline.variables.someVar>.contains(<+pipeline.variables.otherVar>)>
```

#### split() <a href="#split" id="split"></a>

You might use `split()` if you want to extract certain details from a string or create an assertion expression based on a variable.

In this example, assume you have a pipeline variable called `abc` with value `def:ghi`. You might use `split()` like this:

```
<+pipeline.variables.abc.split(':')[1]>
```

This expression evaluates to `ghi`.

For this example, assume you have a pipeline variable called `abc` with value `5.6.0`. You could use `split()` like this:

```
<+pipeline.variables.abc.split('\\.')[1]>
```

This expression resolves to `6`.

To create an assertion expression from a variable, assume you have a variable `someVar` with the value `prod-environment-variable`. To extract `prod` from this value, you would use `split()` like this:

```
<+pipeline.variables.someVar.split('-')[0]>
```

#### charAt() <a href="#charat" id="charat"></a>

Use `charAt()` to return the character at a specified index in a Harness variable expression string.

This can be useful, for example, with an expression such as `<+artifact.tag>` that contains a version number that you want to pull out.

To demonstrate this method, assume you have the expression `<+version>` with the value `1.2.3`.

To get the first character of the string, you would use `charAt()` like this:

```
<+<+version>.charAt(0)>
```

This expression evaluates to `1`.

#### substring() <a href="#substring" id="substring"></a>

To demonstrate the `substring()` method, assume you have a variable `someVar` with the value `prod-environment-variable`. To extract `prod` from this value, you would use `substring()` like this:

```
<+<+pipeline.variables.someVar>.substring(0,3)>
```

### Use multiple methods <a href="#use-multiple-methods" id="use-multiple-methods"></a>

You can also use multiple, nested methods. For example, assume you have a variable called `myvar` with a value `hello`, and you use the methods `substring` and `indexOf`. You could use these methods like this:

```
<+<+stage.variables.myvar>.substring(<+<+stage.variables.myvar>.indexOf("e")>)>
```

This expression evaluates to `ello`.

### Unsupported methods <a href="#unsupported-methods" id="unsupported-methods"></a>

The `getClass()` method is not supported in expressions and it is not evaluated.
