function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Olga Shirokova 5Olga 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:
  • starts with W200
  • can be different length
  • can be followed by a space, comma, other character (not a number) or nothing
Examples:
"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?
Best Answer chosen by Olga Shirokova 5
Malika Pathak 9Malika Pathak 9
This code is perfect for your requirement.
String myString = 'my order W200454545123. test'; 
Integer startIndex = myString.indexOf('W200');
 Integer endIndex =  myString.length();
 for(Integer i = startIndex + 4; i< myString.length() ; i++)
 {     
Integer ascii = myString.charAt(i);    
 if((ascii < 48) || (ascii > 57))
 {       
  endIndex = i;         break;   
  }
 }

String newValue = myString.substring(startIndex, endIndex);
System.debug(newValue);


Please mark it as the best answer if it helps you to fix the issue.

Thank you!

All Answers

Malika Pathak 9Malika Pathak 9
Hi Olga Shirokova,
    
Greetings!
 
String myString = 'my order W200454545 ,tests';
String newValue = '';
Integer startIndex = myString.indexOf('W200');
Integer endSpace = myString.indexOf(' ', startIndex);
Integer endComma = myString.indexOf(',', startIndex);
Integer endIndex;
if(endComma == endSpace) {
    endIndex = myString.length() - 1;
}
else {
    if(endComma == -1) {
        endIndex = endSpace;
    }
    else if(endSpace == -1) {
        endIndex = endComma;
    }
    else if(endSpace < endComma) {
        endIndex = endSpace;
    }
    else if(endComma < endSpace) {
        endIndex = endComma;
    }
}
newValue = myString.substring(startIndex, endIndex);
System.debug(newValue);


Please mark it as the best answer if it helps you to fix the issue.

Thank you!

Regards,
Malika Pathak
Olga Shirokova 5Olga Shirokova 5

@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.

Malika Pathak 9Malika Pathak 9
This code is perfect for your requirement.
String myString = 'my order W200454545123. test'; 
Integer startIndex = myString.indexOf('W200');
 Integer endIndex =  myString.length();
 for(Integer i = startIndex + 4; i< myString.length() ; i++)
 {     
Integer ascii = myString.charAt(i);    
 if((ascii < 48) || (ascii > 57))
 {       
  endIndex = i;         break;   
  }
 }

String newValue = myString.substring(startIndex, endIndex);
System.debug(newValue);


Please mark it as the best answer if it helps you to fix the issue.

Thank you!
This was selected as the best answer