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
ramm12ramm12 

String concatination help urgent...!

hi experts,

I have a string like below,

String s = '(1 OR 2) or 3';

so the above string contains numbers i.e 1,2,3. If number contains means i need to get string like below using apex code,

String s = '(1# OR 2#) or 3#';

Any help much appreciated...!

Thanks in advance...
Vishant ShahVishant Shah
you can simply do 

s.replace ('1', '1#''); and repeat from 0 to 9;

although if you have number 10 it wont help

Ta
Vish


ramm12ramm12
Hi vishant, thanks for your reply.. is it possible to patter matcher object in salesforce
ramm12ramm12
For example in my case if the string come like below means,

String s = '(1 OR 2) or (3 AND 4) Or (5 AND 6)';

In like above case i need to do like hardcoding replace('1','1#').

so how to avoid
Vishant ShahVishant Shah
you can break the string by space and load in a string array, then get the number in each array and add the # and replace the array value , once this is complete reconstruct the string from the array in order


ramm12ramm12
can you send me the sample code
Vishant ShahVishant Shah
I wont be able to work on sample code i only plotted down how i thought it might work ..
praveen murugesanpraveen murugesan
Hi Ram,

Try this, 

you can split the string by this way.. 

string[] a = 'aaa21h32j2'.split('(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)');
system.debug('#####'+a); 

OUTPUT : #####(aaa, 21, h, 32, j, 2)

so after this use loop and check whether a[0] is string or integer if integer add # or any other as you wish. Finaly add all to string.
eg:

a[0] is integer then a[0]=a[0]+'#';


Mark this as best answer if its helps.

Thanks.