&amp replace the string in apex string.unescapeHTML4() is not working - salesforce

This code is storing the stage name in string format but one of the value contain & and because of it it showing &amp by replacing the string value
string stages = ''; //string type stages variable
for (string s :stagenames ){ //loop
stages = +stages+s+':'; //assigning value to the stages
System.debug(stages.unescapeHTML4()); //getting all the values inside it one by one
}//but one string is jack & Jill so instead of showing actual string it just showed me 'jack &amp'

Related

How to properly trim string

I am trying to separate an entered name into 2 strings.
Each name is entered in the convention of lastName, firstName
or Ex: Smith, John
I would like to separate the name into a lastName and firstName variable by trimming the string before and after the comma and space.
I have tried
Dim nameSeparator() As Char = {",", " "}
Dim lastName = txtEditName.Text.TrimEnd(nameSeparator)
Dim firstName = txtEditName.Text.TrimStart(nameSeparator)
But after running this, lastName and firstName both equal the full string from txtEditName.Text
If you want to "split" a string in two substrings using a certain separator then you should use the proper method: string.Split
Only after the splitting you can remove unneeded characters at begin or end of a string using the string.Trim method
Dim input() as String = txtEditName.Text.Split(",")
Dim lastName = input(0).Trim()
Dim firstName = input(1).Trim()
Of course this example assumes that you have exactly the input described in your question. If you want to use this approach in a real application then you should check if the result from the splitting produces exactly two substring before trying to access the substrings

resolving string (read from database) to list python

I have a list stored in database as varchar. When I read the list value, it is read as string. I'm trying to convert string to a valid list
Here are 2 example lists I have
str_db_bins = [10,20,30,40] (This is string)
The desired output is
str_db_bins_list = [10,20,30,40] (This is list)
I want to convert this to list. When I use split method I get [[,1,0...]] which is not the desired output
2nd situtation is
str_db_lov = ['id','num','val'].
When I use split method, again I get weird output
The desired output is to read this as list
str_db_lov_list = ['id', 'num', 'val']
How do I deal with these situations
Thanks
Pari
str_db_bins_list = eval(str_db_bins)
str_db_lov_list = eval(str_db_lov)

User Profile Privacy screen with comma separated String

I have a listView with many fields and CheckBox next to each field. Screen similar to a FaceBook Profile screen with Privacy setting to each field. So friends can not see those fields if marked as Private.
On selection of CheckBox, i have to create a comma separated String.
Example,
FirstName Text ---> isFirstNamePrivate boolean
LastName Text ---> isLastNamePrivate boolean
...
I have to create a
String str = "FirstName,LastName"
if both are marked as Private.
If only isFirstNamePrivate is true then
String str = "FirstName"
Also if i receive a comma separated String from Service, with that i have to make those Boolean array.
Given::
String[] fieldNamesArray = "field1","field2","field3","field4","field5"};
Boolean[] isfieldPrivate = {true,false,true,false,true};
// fieldNamesArray.length will be equal to isfieldPrivate.length
Need to create below commaSeparatedStr from above given arrays.
//
String commaSeparatedStr = "field1,field3,field5";
Question is:
1) What is the optimized way to create a comma separated String.
2) What is the optimized way to create the Boolean array from the commaSeparatedString avoiding for loop on commaSeparatedStr .contains(str[n])
Given::
String[] fieldNamesArray = "field1","field2","field3","field4","field5"};
String commaSeparatedStr = "field1,field3,field5";
Need to create below Boolean array with commaSeparatedStr from above 2 arrays.
Boolean[] isfieldPrivate = {true,false,true,false,true};
// fieldNamesArray.length will be equal to isfieldPrivate.length
//
Where is the question part here?
If you are asking for a suggestion:
If there are 5 fields and first 3 are selected, make your string like
{1,1,1,0,0}
and pass it to your webservice. This would lighten the data package size.

google visualization data.addRow - get data out of html data attribute

I have data inside a data attribute, like so:
<div class="dashboard-module" data-rows="new Date(2013,10,04),12,"OR"##new Date(2013,10,17),2,"OR"##new Date(2013,10,09),2,"CA""></div>
Im trying to split this string up and use it in the data.addRow function:
rows = el.data('rows');
rowsarray = rows.split('##');
// Error: Row given with size different than 3 (the number of columns in the table).
$.each(rowsarray, function(index, value) {
data.addRow( [value] );
});
// the following works
data.addRow([new Date(2013,10,04),12,"OR"]);
data.addRow([new Date(2013,10,09),2,"CA"]);
data.addRow([new Date(2013,12,12),14,"AL"]);
I guess the commas inside the new date are being counted as different parts of the array?
I'm assuming that the double-quotes inside your data-rows attribute are escaped (otherwise the HTML is malforned).
When you call rowsarray = rows.split('##');, you are getting an array of strings, like this:
[
'new Date(2013,10,04),12,"OR"',
'new Date(2013,10,17),2,"OR"',
'new Date(2013,10,09),2,"CA"'
]
not an array of arrays. If you want to store your data in an HTML attribute, your best bet is to use a JSON-compatible format. The problem then becomes storing dates, since Date objects are not JSON-compatible, but that is easy to work around. Store your data like this instead:
[["Date(2013,10,04)",12,"OR"],["Date(2013,10,17)",2,"OR"],["Date(2013,10,09)",2,"CA"]]
I did two things with the data-rows attribute: first, I changed the dates from a format like new Date(2013,10,17) to a string like "Date(2013,10,17)". Second, I converted the string to a JSON string representation of an array of arrays (which uses the standard javascript array brackets [ and ]). Note that JSON requires the use of double-quotes for all internal strings, so you must either escape all internal strings to use with the data-rows attribute, or use single-quotes around the data-rows attribute string (eg: data-rows='<string>').
You can then parse that string for entry into your DataTable:
rows = JSON.parse(el.data('rows'));
// convert date strings to Date objects
for (var i = 0; i < rows.length; i++) {
var dateStr = rows[i][0];
var dateArr = dateStr.substring(5, dateStr.length - 1).split(',');
rows[i][0] = new Date(dateArr[0], dateArr[1], dateArr[2]);
}
data.addRows(rows);

Join an array of strings in Apex

Using Apex, I want to split a string and then rejoin it with the 'AND' operator as the separator.
I split the string successfully but having an issue in rejoining it.
String [] ideaSearchText = searchText.Split(' ');
// How to rejoin the array of strings with 'AND'?
How can I do this?
You can do this as of v26 (Winter 13) by passing your String[] to String.join().
String input = 'valueOne valueTwo valueThree';
String[] values = input.split(' ');
String result = String.join( values, ' AND ' );
Anonymous Apex output calling System.debug(result):
21:02:32.039 (39470000)|EXECUTION_STARTED
21:02:32.039 (39485000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
21:02:32.040 (40123000)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>()
21:02:32.040 (40157000)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>()
21:02:32.040 (40580000)|USER_DEBUG|[5]|DEBUG|valueOne AND valueTwo AND valueThree
Note if the string object is too large you will get the exception Regex too complicated. In this case you can do something like the following:
Blob blobValue = (Blob)record.get(blobField);
// Truncate string then split on newline, limiting to 11 entries
List<String> preview = blobValue.toString().substring(0,1000).split('\n', 11);
// Remove the last entry, because The list’s last entry contains all
// input beyond the last matched delimiter.
preview.remove(preview.size()-1);
// In my use-case, I needed to return a string, and String.join() works
// as the reverse of split()
return String.join(preview, '\n');

Resources