string

Which style of Ruby string quoting do you favour?

Which style of Ruby string quoting do you favour? Up until now I've always used 'single quotes' unless the string contains certain escape sequences or interpolation, in which case I obviously have to use "double quotes". However, is there really any reason not to just use double quoted strings everywhere? ...

What is meant by immutable?

This could be the dumbest question ever asked but I think it is a total confusion for a newbie. Can somebody clarify what is meant by immutable? Why is a String immutable? What are the advantages/disadvantages of immutable objects? Why should a mutable object such as StringBuilder be preferred over String and vice-versa? A nice example (...

Calculating the minimum width needed to show text in X lines, in .Net?

How could you calculate the minimum width needed to display a string in X lines, given that text should break on whitespace? ...

endsWith in javascript

How can I check if a string ends with a particular character in javascript? example I have a string say var str = "mystring#"; I want to know if that string str is ending with "#". How can I check it? is there a endsWith() method in javascript? one solution I have is take the length of the string and get the last character and check i...

Case insensitive string replacement in JavaScript?

I need to highlight, case insensitively, given keywords in a JavaScript string. For example: highlight("foobar Foo bar FOO", "foo") should return "<b>foo</b>bar <b>Foo</b> bar <b>FOO</b>" I need the code to work for any keyword, and therefore using a hardcoded regular expression like /foo/i is not a sufficient solution. What is the...

How to rewrite this block of code using a StringBuilder in Java?

Given a word, I've to replace some specific alphabets with some specific letters such as 1 for a, 5 for b etc. I'm using regex for this. I understand that StringBuilder is the best way to deal with this problem as I'm doing a lot of string manipulations. Here is what I'm doing: String word = "foobooandfoo"; String converted = ""; conver...

String operation optimisation in C#

The following C# code takes 5 minutes to run: int i = 1; string fraction = ""; while (fraction.Length < 1000000) { fraction += i.ToString(); i++; } "Optimising it" like this causes it to run in 1.5 seconds: int i = 1; string fraction = ""; while (fraction.Length < 1000000) { // concatenating strings is much faster for sma...

LINQ Distinct operator, ignore case?

Given the following simple example: List<string> list = new List<string>() { "One", "Two", "Three", "three", "Four", "Five" }; CaseInsensitiveComparer ignoreCaseComparer = new CaseInsensitiveComparer(); var distinctList = list.Distinct(ignoreCaseComparer as IEqualityComparer<string>).ToList(); It appears the CaseInsensit...

Does JavaScript support verbatim strings?

In C# you can use verbatim strings like this: @"\\server\share\file.txt" Is there something similar in JavaScript? ...

Best way to break long strings in C# source code

I am wondering what is the "best practice" to break long strings in C# source code. Is this string "string1"+ "string2"+ "string3" concatenated during compiling or in run time? ...

string to const char*

This line: strcat(query,*it); (where *it is an iterator to a string) Keeps giving me this error: no matching function for call to \`strcat(char[200], const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)' I guess it's because strcat takes in a char* while *it is a string. How do I convert it from a str...

C# Build hexadecimal notation string

Hi, How do I build an escape sequence string in hexadecimal notation. Example: string s = "\x1A"; // this will create the hex-value 1A or dec-value 26 I want to be able to build strings with hex-values between 00 to FF like this (in this example 1B) string s = "\x" + "1B"; // Unrecognized escape sequence Maybe there's another way o...

How can I insert a character into a string with the s/// operator?

I'm trying to insert a comment character into a string something similar to this: -CreateVideoTracker VT1 "vt name" becomes -CreateVideoTracker VT1 # "vt name" The VT1 word can actually be anything, so I'm using the regex $line =~ s/\-CreateVideoTracker \w/\-CreateVideoTracker \w # /g; which gives me the result: -CreateVideoTra...

How to split a web address

So I'm using python to do some parsing of web pages and I want to split the full web address into two parts. Say I have the address http://www.stackoverflow.com/questions/ask. I would need the protocol and domain (e.g. http://www.stackoverflow.com) and the path (e.g. /questions/ask). I figured this might be solved by some regex, however ...

How can you find the number of occurrences of a particular character in a string using sql?

How can you find the number of occurrences of a particular character in a string using sql? Example: I want to find the number of times the letter ‘d’ appears in this string. declare @string varchar(100) select @string = 'sfdasadhfasjfdlsajflsadsadsdadsa' ...

Is StringBuilder.Replace() more efficient than String.Replace?

If you have to use String.Replace() to replace test 50 times, you essentially have to create a new string 50 times. Does StringBuilder.Replace() do this more efficiently? E.g., should I use a StringBuilder if I'm going to be replacing a lot of text, even while I won't be appending any data to it? I'm using .NET, but I assume this woul...

How can I convert a string to a number in Perl?

How would I convert a string holding a number into an integer in Perl? ...

strtok and replace a substring in C++

If I have a string "12 23 34 56" What's the easiest way to change it to "\x12 \x23 \x34 \x56"? ...

Using strtok with a string argument (instead of char*)?

I have a string that I would like to tokenize. But the strtok() function requires my string to be a char*. How can I do this quickly? token = strtok(str.c_str(), " "); fails because it turns it into a const char*, not a char* ...

vb6 How would I edit this to make the extension only count to 3 characters(string parsing)

Someone posted a great little function here the other day that separated the full path of a file into several parts that looked like this: Function BreakDown(Full As String, FName As String, PName As String, Ext As String) As Integer If Full = "" Then BreakDown = False Exit Function End If If InStr(Full, "\") Then FName = Full PN...