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
Andrei Ionut MilitaruAndrei Ionut Militaru 

String processing

Hello guys,
I'm trying to find out if there's a way to remove a certain number of characters from a string.
Challenge is, let's say we have string str = 'I get 7 hours of sleep'. I want to remove 'I get x' meaning that I want to remove 'I get' ( which will always be 'I get' ) and another 2 characters ( whatever they are, including the space between ).
I'm trying to do this with:
str.removeStartIgnoreCase('I GET '); - but can't find a way to tell the code about the next 2 characters, which I want removed from str.
Does this make sense?
Best Answer chosen by Andrei Ionut Militaru
PradeepgorePradeepgore
Hi Andrei Ionut Militaru !!

Try this , 
String Str='I get 7 hours of sleep';
String refinedStr=str.remove('I get') ;
System.debug('**refined Str->'+refinedStr);
//7 hours of sleep
refinedStr=refinedStr.substring(2,refinedStr.length() );
System.debug('**Further Refined ->'+refinedStr) ;
// hours of sleep

 

All Answers

PradeepgorePradeepgore
Hi Andrei Ionut Militaru !!

Try this , 
String Str='I get 7 hours of sleep';
String refinedStr=str.remove('I get') ;
System.debug('**refined Str->'+refinedStr);
//7 hours of sleep
refinedStr=refinedStr.substring(2,refinedStr.length() );
System.debug('**Further Refined ->'+refinedStr) ;
// hours of sleep

 
This was selected as the best answer
Andrei Ionut MilitaruAndrei Ionut Militaru
Hi Pradeepgore,
I've ended up doing something pretty similar right after posting it on the community and fixed it.
Thanks a lot for your answer, I've marked it in case anybody else is having this issue.
Cheers.