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
Joe HayesJoe Hayes 

Strings List to Map of Strings and Integers

This should be easy but I can't find a good answer for it.

I have a string e.g.
String text = 'Send Email (3 Days Prior); Laptops (5 Days Prior); Make Phone call (2 Days Prior)';

What I need to end up with is a list or map or something that has String and Integers

The above text will then be in a table like this so I can query and fetch values.

String                    Integer
Send Email              3
Laptops                    5
Make Phone Call     2

At the moment I am doing the following:
List<String> initialList = new List<String>();
        initialList.addAll(venueTasks.split(';'));

        String initialListString = initialList.toString();
initialListString = initialListString.replaceAll(' (',';');
initialListString = initialListString.replaceAll(' Days Prior)','');

This then gives me SendEmail;3;Laptops;5;etc

I'm just not sure how to do this properly and get it into a table.

The end goal is to for loop over the list/map and create tasks where subject is the string and activity date is system.today() + integer

Thanks

 
Best Answer chosen by Joe Hayes
Suraj Tripathi 47Suraj Tripathi 47
Hi  Joe Hayes ,

I have solved your problem in my way and hope so I full filled all your requirments.
You can have a look in below code.
String text = 'Send Email (3 Days Prior); Laptops (5 Days Prior); Make Phone call (2 Days Prior)';
map<String,Integer> SubjectVsInt = new map<String,Integer>();
map<String,date> SubjectVsDate = new map<String,date>();
for(String s:text.split(';'))
{
    String delimiter='(';
    String Key=s.substringBefore(delimiter); 
    String value=(s.substringAfter(delimiter)).substringBefore('Days Prior)'); 
    System.debug('Key---> '+Key);
    System.debug('Value---> '+value);
    SubjectVsInt.put(Key,Integer.valueOf(value.trim()));
    SubjectVsDate.put(Key,System.today().addDays(Integer.valueOf(value.trim())));
    System.debug(SubjectVsDate);
}
I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.
Thanks and Regards,
Suraj Tripathi

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi  Joe Hayes ,

I have solved your problem in my way and hope so I full filled all your requirments.
You can have a look in below code.
String text = 'Send Email (3 Days Prior); Laptops (5 Days Prior); Make Phone call (2 Days Prior)';
map<String,Integer> SubjectVsInt = new map<String,Integer>();
map<String,date> SubjectVsDate = new map<String,date>();
for(String s:text.split(';'))
{
    String delimiter='(';
    String Key=s.substringBefore(delimiter); 
    String value=(s.substringAfter(delimiter)).substringBefore('Days Prior)'); 
    System.debug('Key---> '+Key);
    System.debug('Value---> '+value);
    SubjectVsInt.put(Key,Integer.valueOf(value.trim()));
    SubjectVsDate.put(Key,System.today().addDays(Integer.valueOf(value.trim())));
    System.debug(SubjectVsDate);
}
I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.
Thanks and Regards,
Suraj Tripathi
This was selected as the best answer
Joe HayesJoe Hayes
Thanks Suraj!

Works perfectly