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
vikrant kumarvikrant kumar 

need to transfer values into map

I have to move values with in brackets  of string s into map having s1,s3,s4 as keys and 40,98,78 as values.how to do that?

i am trying the following code in workbench.

string s='(s1,40),(s3,98),(s4,78)';
list<string> k=s.replace(')','').replace('(','').split(',');
map<string,string> maps=new map<string,string>();
for(integer i=0;i<k.size();i++){
maps.put('k[i]','k[i++]');
}
system.debug('size of map---------'+ maps.size());

Is there any better way to do this?please let me know?

In workbench the size of map shows 1 when executed.But it is 3.is some thing wrong with my logic. 
Best Answer chosen by vikrant kumar
Anirudh SinghAnirudh Singh
No Problem. Please can you mark the question as solved. Thanks.

All Answers

Anirudh SinghAnirudh Singh
Hi Vikrant,

There is a mistake in your code. Please try below, it works fine:

String s='(s1,40),(s3,98),(s4,78)';
List<String> k=s.replace(')','').replace('(','').split(',');
Map<String, String> maps=new map<String, String>();

for(integer i=0;i<k.size();i++)
{
    maps.put(k[i], k[i+1]);
    i++;
}
system.debug('size of map---------'+ maps.size());// It will give 3 now.

Please let me know if it helps.

Thanks and Regards,
Anirudh Singh
vikrant kumarvikrant kumar
thank u very much.its working
Anirudh SinghAnirudh Singh
No Problem. Please can you mark the question as solved. Thanks.
This was selected as the best answer