string
Does jQuery strip some html elements from a string when using .html()?
After a few quick tests it seems do me that this behavior isn’t caused by jQuery but instead by the browser. As you can easily verify yourself (DEMO http://jsbin.com/ocupa3) var data = “<html><head><title>Untitled Document</title></head><body><p>test</p></body></html>”; data = $(‘<div/>’).html(data); alert(data.html()); yields different results in different browsers Opera 10.10 <HEAD><TITLE>Untitled Document</TITLE></HEAD><P>test</P> FF 3.6 <title>Untitled Document</title><p>test</p> IE6 <P>test</P> so … Read more
Find and replace nth occurrence of [bracketed] expression in string
Here is another possible solution. You can pass the string.replace function a function to determine what the replacement value should be. The function will be passed three arguments. The first argument is the matching text, the second argument is the position within the original string, and the third argument is the original string. The following … Read more
Python replace function [replace once]
I’d probably use a regex here: >>> import re >>> s = “The scary ghost ordered an expensive steak” >>> sub_dict = {‘ghost’:’steak’,’steak’:’ghost’} >>> regex = ‘|’.join(sub_dict) >>> re.sub(regex, lambda m: sub_dict[m.group()], s) ‘The scary steak ordered an expensive ghost’ Or, as a function which you can copy/paste: import re def word_replace(replace_dict,s): regex = ‘|’.join(replace_dict) … Read more
How to correctly compute the length of a String in Java?
The normal model of Java string length String.length() is specified as returning the number of char values (“code units”) in the String. That is the most generally useful definition of the length of a Java String; see below. Your description1 of the semantics of length based on the size of the backing array/array slice is … Read more
Convert String to Double – VB
Try looking at Double.TryParse() if you are using .NET 1.1/2.0/3.0/3.5/4.0/4.5
jQuery remove tag from HTML String without RegEx
You can wrap your string in a jQuery object and do some sort of a manipulation like this: var removeElements = function(text, selector) { var wrapped = $(“<div>” + text + “</div>”); wrapped.find(selector).remove(); return wrapped.html(); } USAGE var removedSpanString = removeElements(“<span>Some Text</span> Some other Text”, “span”); The beauty of this approach is that you can … Read more
Java: Checking equality of arrays (order doesn’t matter)
Arrays.sort(s1); Arrays.sort(s2); Arrays.equals(s1,s2); In case you do not want to modify the original arrays Arrays.equals( Arrays.sort( Arrays.copyof(s1,s1.length)), Arrays.sort( Arrays.copyof(s2,s2.length)) ); Arrays.sort() uses an optimized quick sort which is nlog(n) for average but O(n2) in worst case. From the java docs. So the worst case it will O(n2) but practically it will be O(nlogn) for most … Read more
C# – What does “\0” equate to?
‘\0’ is a “null character“. It’s used to terminate strings in C and some portions of C++. Pex is doing a test to see how your code handles the null character, likely looking for the Poison Null Byte security exploit. Most C# code has nothing to fear; if you pass your string to unmanaged code, … Read more