You need to sign in to do that
Don't have an account?
Olga Shirokova 5
How to get a substring that can be different length and can be followed by different character or no character
Hello,
I need to get a substring from a text. Conditions:
"Test W200123456 test" -> W200123456
"Test W200123" -> W200123
"W200123456789, test" -> W200123456789
I have a code that works if the substring i need is followed by a space.
String myString= 'my order W200454545 tests';
Integer index = myString.indexOf('W200');
Integer index2 = myString.indexOf(' ', index);
String newValue = myString.substring(index, index2);
How can I account for all other options?
I need to get a substring from a text. Conditions:
- starts with W200
- can be different length
- can be followed by a space, comma, other character (not a number) or nothing
"Test W200123456 test" -> W200123456
"Test W200123" -> W200123
"W200123456789, test" -> W200123456789
I have a code that works if the substring i need is followed by a space.
String myString= 'my order W200454545 tests';
Integer index = myString.indexOf('W200');
Integer index2 = myString.indexOf(' ', index);
String newValue = myString.substring(index, index2);
How can I account for all other options?
Please mark it as the best answer if it helps you to fix the issue.
Thank you!
All Answers
Greetings!
Please mark it as the best answer if it helps you to fix the issue.
Thank you!
Regards,
Malika Pathak
@Malika
Thank you for your response but this does not work if there is nothing after the order number (for example "Test W200454545"), if there is some other character ("Test W200454545; test") or there is a dot and some other text ("my order W200454545123. test").
The order number can be anywhere in the text.
Please mark it as the best answer if it helps you to fix the issue.
Thank you!