ShowAll Questionssorted byDate Posted
Ezmo

# If function "if starts with"

Hi all,

Just a quick one which I'm sure is really easy but I've been wracking my brains for a few hours on this one.

I want to do an if function that would read along the lines of "If string x starts with string y".

Any help would be great!

Many thanks

Eran

SLockard

Also, there are other ways, one is much easier too.

```String x = 'ABCDEFGhello';
String y = 'ABCDEFG';

Boolean result = x.startsWith(y);
system.debug(result);

if (result)
{
system.debug('yes');
}
else
{
system.debug('no');
}

Integer r = x.indexof(y);

if (r == 0)
{
system.debug('yes');
}
else
{
system.debug('no');
}```

Use whichever one you'd like.

SLockard

Here's some logic that works, hope this helps!

```String x = 'ABCDEFGhello';
String y = 'ABCDEFG';

String result = x.substring(0,y.length());
system.debug(result);

if (result == y)
{
system.debug('yes');
}
else
{
system.debug('no');
}```

SLockard

Also, there are other ways, one is much easier too.

```String x = 'ABCDEFGhello';
String y = 'ABCDEFG';

Boolean result = x.startsWith(y);
system.debug(result);

if (result)
{
system.debug('yes');
}
else
{
system.debug('no');
}

Integer r = x.indexof(y);

if (r == 0)
{
system.debug('yes');
}
else
{
system.debug('no');
}```

Use whichever one you'd like.

This was selected as the best answer
Ezmo

This works nicely, thanks muchly! :)

I'll get all this logic stuff one day lol.