string-manipulation

Python: is using "..%(var)s.." % locals() a good practice ?

I discovered this pattern (or anti-pattern) and I am very happy with it. I feel it is very agile: def example(): age = ... name = ... print "hello %(name)s you are %(age)s years old" % locals() Sometimes I use its cousin: def example2(obj): print "The file at %(path)s has %(length)s bytes" % obj.__dict__ I don't ne...

Prepending a string to another string

Currently, I have this code to prepend a "tag" to an exception message which gives me a very light version of a stack trace: try { doSomething(); } catch (std::exception& e) { int size = 8 + _tcslen(e.what()); TCHAR* error = new TCHAR[size]; _sntprintf(error, size, TEXT("myTag: %s"), e.what()); std::exception x = std...

How to perform this RegExp in Python ?

Hello, I would like to transform a phone number of this form +33.300000000 in 03.00.00.00.00 +33 is the indicatif it could be 2 or 3 digits length. Digits after the . are the phone number. It could be 9 or 10 digits length. I try like this : p = re.compile( "\+[0-9]+\.([0-9]+)", re.VERBOSE) number = "+33.300000000" p.sub("0\1", numb...

PHP: String formatting with printf

I am trying to write a quick string formatting routine to take an unformatted ISRC code and add hyphenation where it is required. For example, the ISRC USMTD9203901 should translate to US-MTD-92-03901. The pattern is: [A-Z]{2}-[A-Z]{3}-[0-9]{2}-[0-9]{5} I have been trying to implement this with substr and this has produced the follow...

msbuild string upper case comparison

Hi, i have the following code in my build-file: <Error Text="Some Text" condition="'$(StringName)' != 'Test'/> It causes an error if the condition is not match. When $(StringName) is 'test' the condition is not met so the error is executed. How can I change the condition, that 'test' als meets the condition? Is there any upper-case ...

PHP Wrap a string in double quotes

I'm attempting to wrap a user-inputted string in double quotes for output but I want to make sure I don't end up with double double quotes on either side of the string. For example, if a user posts "Hello" I don't want to turn it into ""Hello"" I can do this fairly easily using the code below, however I'm concerned that this...

How to extract words from a sentence efficiently in C?

I need an efficient function that extracts first second and rest of the sentence into three variables. ...

Automate FAQ system & Algorithm

I'm planning to automate the FAQ section in my site where the questions and answers stored in standard DB and would like to get the input question from the user and recognize it (algorithm) and get the appropriate answer for that and return it to the user. Approach : get input string -> parse -> check words with each question in DB ->re...

string replacing - C#

Hi, Say I have a string containing numbers and other chars. I want to reduce the string to numbers only. F.e. from 23232-2222-d23231 to 23232222223231 Can this be done with string.replace()? if not, what's the simplest and shortest way? 10x! ...

Break string into separate lines: a showdown

Accommodating legacy database tables that were designed specifically for the IBM mainframe screens they represent has caused me much aggravation. Often, I find the need to break a string into multiple lines to fit the table column width as well as the users who are still viewing the data with terminal emulators. Here are two functions I'...

Best way to merge hex strings in c++? [heavily edited]

I have two hex strings, accompanied by masks, that I would like to merge into a single string value/mask pair. The strings may have bytes that overlap but after applying masks, no overlapping bits should contradict what the value of that bit must be, i.e. value1 = 0x0A mask1 = 0xFE and value2 = 0x0B, mask2 = 0x0F basically says that the...

Deleting portion of a text string in PHP

I've constructed a form, but some rows of the form can potentially be returned blank, with default values. I'm trying to find a way of searching the form output and then deleting the bit which I know is not needed - which looks like: <tr bgcolor="#FFFFFF"> <td>2E</td> <td id="8003">-800</td> </tr> I've used str_replace() effectively o...

How to convert long to LPCWSTR?

Hi, how can I convert long to LPCWSTR in C++? I need function similar to this one: LPCWSTR ToString(long num) { wchar_t snum; swprintf_s( &snum, 8, L"%l", num); std::wstring wnum = snum; return wnum.c_str(); } ...

Is conversion to String using ("" + <int value>) bad practice?

Is conversion to String in Java using "" + <int value> bad practice? Does it have any drawbacks compared to String.valueOf(...)? Code example: int i = 25; return "" + i; vs: int i = 25; return String.valueOf(i); Update: (from comment) And what about Integer.toString(int i) compared to String.valueOf(...)? ...

function to split a filepath into path and file

Lets say I have a function: void split_path_file(char** p, char** f, char *pf) { //malloc and set *p to file path, malloc and set *f to file name //pf is the pointer to the full file and path "C:\sponge\bob\square.pants" // edit: leave pf in its origional state } Whats the best way to accomplish this? ...

Standard Template String class: string.fill()

I need a way to create a string of n chars. In this case ascii value zero. I know I can do it by calling the constructor: string sTemp(125000, 'a'); but I would like to reuse sTemp in many places and fill it with different lengths. I am calling a library that takes a string pointer and length as an argument and fills the string with ...

Get the first few words(100 or 200) from a long summary(plain string or html) using c#?

I want to get the first few words(100 or 200) from a long summary of words (plain string or html) using c#. My requirement is to display the short description of the long summary of content(this content may include html elements). I'm able to retrieve the plain string but when it is html, the elements are cut it between Example, I get l...

Manipulating a byte array

I have a simple byte array ["\x01\x01\x04\x00"] I'm not sure how I can alter just the second value in the string (I know the array only has one item), whilst still keeping the object a byte array. Something along these lines: ["\x01#{ARGV[0]}\x04\x00"] ...

How to find last occurence of a number in a string using Ruby?

Using Ruby... Given a String x = "blah_blah.do.dah[4543]junk_junk" How do I remove all text after the last number/digit? I thought the easiest way to do this might be by finding the index of last occurence and then removing everything after that index. However, I can't seem to figure out how to obtain that index. All my attempts at u...

string comparison in ie

Hi, Following is my code, i want to hide a li tag depending on its content. dom.find('ul#overlay_opt li').click(function(){ if($(this).attr('class') != 'deactive'){ var content = $(this).text(); if(content == 'none') dom.find('#secondary_para').hide(); else dom.find('#secondary_para').html($(this).text()).show(); } }); ...