string

Integer formatting, padding to a given length

I need to pad the output of an integer to a given length. For example, with a length of 4 digits, the output of the integer 4 is "0004" instead of "4". How can I do this in C# 2.0? ...

What is the easiest/best/most correct way to iterate through the characters of a string in Java?

StringTokenizer? Convert the String to a char[] and iterate over that? Something else? ...

How do I emulate Python's named printf parameters in Ruby?

In Python, you can do this: print "Hi! I'm %(name)s, and I'm %(age)d years old." % ({"name":"Brian","age":30}) What's the closest, simplest Ruby idiom to replicate this behavior? (No monkeypatching the String class, please.) EDIT: One of the really excellent benefits of this is that you can store the pre-processed string in a varia...

What is the best way to trim() in javascript

The question says it all; JS doesn't seem to have a native trim() method. ...

Dealing with a string containing multiple character encodings.

I'm not exactly sure how to ask this question really, and I'm no where close to finding an answer, so I hope someone can help me. I'm writing a Python app that connects to a remote host and receives back byte data, which I unpack using Python's built-in struct module. My problem is with the strings, as they include multiple character e...

How do I reverse a UTF-8 string in place?

Recently, someone asked about an algorithm for reversing a string in place in C. Most of the proposed solutions had troubles when dealing with non single-byte strings. So, I was wondering what could be a good algorithm for dealing specifically with utf-8 strings. I came up with some code, which I'm posting as an answer, but I'd be glad ...

Strange cross-threading UI errors

I'm writing a WinForms app which has two modes: console or GUI. Three projects within the same solution, one for the console app, one for the UI forms and the third to hold the logic that the two interfaces will both connect too. The Console app runs absolutely smoothly. A model which holds the user-selections, it has an IList<T> wher...

How do I split strings?

How do I split strings in J2ME in an effective way? There is a StringTokenizer in the standard edition, but it is absent in the micro edition. ...

escaping formatting characters in java String.format

This question is pretty much the same as this .Net question exept for java. How do you escape the %1$ characters in a java string.format? THe reason I need to do this is that I'm building up a string that will later have more info inserted into it. I've thought of having one of the args just be "%1$" but that doesn't seem to be very el...

Is there a simple script to convert C++ enum to string?

Suppose we have some named enums: enum MyEnum { FOO, BAR = 0x50 }; What I googled for is a script (any language) that scans all the headers in my project and generates a header with one function per enum. char* enum_to_string(MyEnum t); And a implementation with something like this: char* enum_to_string(MyEnum t){ ...

Repeat String - Javascript

What is the best or most concise method for returning a string repeated an arbitrary amount of times? The following is my best shot so far: function repeat(s, n){ var a = []; while(a.length < n){ a.push(s); } return a.join(''); } ...

Is there a Perl function to turn a string into a regexp to use that string as pattern?

I have trouble using Perl grep() with a string that may contain chars that are interpreted as regular expressions quantifiers. I got the following error when the grep pattern is "g++" because the '+' symbols are interpreted as quantifiers. Here is the output of for program that follows: 1..3 ok 1 - grep, pattern not found ok 2 - grep,...

How do I split a string on a fixed character sequence?

Suppose I have following string: String asd = "this is test ass this is test" and I want to split the string using "ass" character sequence. I used: asd.split("ass"); It doesn't work. What do I need to do? ...

The most sophisticated way for creating comma-separated Strings from a Collection/Array/List?

During my work with databases i noticed that i write query strings and in this strings i have to put several restrictions in the where-clause from a list/array/collection. Should looks like this: select * from customer where customer.id in (34, 26, ..., 2); You can simplify this by reducing this to the question that you have collecti...

What is the best way to slurp a file into a string in Perl?

Yes, There's More Than One Way To Do It but there must be a canonical or most efficient or most concise way. I'll add answers I know of and see what percolates to the top. To be clear, the question is how best to read the contents of a file into a string. One solution per answer. ...

How to find difference between two strings?

I have two strings and would like to display the difference between them. For example, if I have the strings "I am from Mars" and "I am from Venus", the output could be "I am from Venus". (Typically used to show what changed in an audit log, etc.) Is there a simple algorithm for this? I am using C# but I guess a generic algorithm could ...

Convert hex string to int in Python

How do I convert a hex string to an int in Python? I may have it as "0xffff" or just "ffff". ...

Regex multi word search

What do I use to search for multiple words in a string? I would like the logical operation to be AND so that all the words are in the string somewhere. I have a bunch of nonsense paragraphs and one plain English paragraph, and I'd like to narrow it down by specifying a couple common words like, "the" and "and", but would like it match ...

Fastest way to do a case-insensitive substring search in C/C++?

I need to do a fast case-insensitive substring search in C/C++. My requirements are as follows: Should behave like strstr() (i.e. return a pointer to the match point). Must be case-insensitive (doh). Must support the current locale. Must be available on Windows (MSVC++ 8.0) or easily portable to Windows (i.e. from an open source librar...

Is making an empty string constant worth it?

I have a cow-orker that swears by //in a singleton "Constants" class public static final String EMPTY_STRING = ""; in a constants class available throughout the project. That way, we can write something like if (Constants.EMPTY_STRING.equals(otherString)) { ... } instead of if ("".equals(otherString)) { ... } I say it's...