string-manipulation

Why having both str_replace() and preg_replace()?

I came to know PHP after Perl, so when I first found preg_* function I basically just used those. Later I read that str_replace() is faster when dealing with literal text. So my question is, can't preg_replace() be as efficient as str_replace() when the search pattern does not use special characters? Maybe just analyzing the pattern to c...

Match similar names in C#

I'm currently working on a project that requires me to match our database of Bands and venues with a number of external services. Basically I'm looking for some direction on the best method for determining if two names are the same. For Example: Our database venue name - "The Pig and Whistle" service 1 - "Pig and Whistle" service 2 - ...

CamelCase to underscores and back in Objective-C

I'm looking for a simple, efficient way to convert strings in CamelCase to underscore notation (i.e., MyClassName -> my_class_name) and back again in Objective C. My current solution involves lots of rangeOfString, characterAtIndex, and replaceCharactersInRange operations on NSMutableStrings, and is just plain ugly as hell :) It seems ...

Mass string replace in python?

Say I have a string that looks like this: str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog" You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these characters with an appropriate value that I have in a dictionary, like so: dict =...

Can I get a non-const C string back from a C++ string?

Const-correctness in C++ is still giving me headaches. In working with some old C code, I find myself needing to assign turn a C++ string object into a C string and assign it to a variable. However, the variable is a char * and c_str() returns a const char []. Is there a good way to get around this without having to roll my own function ...

Filtering "whitespace-only" strings in JavaScript

I have a textbox collecting user input in my JS code. I would like to filter junk input, like strings that contain whitespaces only. In C#, I would use the following code: if (inputString.Trim() == "") Console.WriteLine("white junk"); else Console.WriteLine("Valid input"); Do you have any recommendation, how to do the same in JavaSc...

How to mimic StackOverflow Auto-Link Behavior

With PHP how can I mimic the auto-link behavior of StackOverflow (which BTW is awesomely cool)? For instance, the following URL: http://www.stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior Is converted into this: <a title="how to mimic stackoverflow auto link behavior" rel="nofollow" href="http://...

Should I use a parser/lexer for this?

I'd like to know if a tool like ANTLR would be appropiate for parsing these rules or it's overkill and I should make my own parser. This is for parsing someone else's format, so I can't change it. It's for fun and to practice, so don't fret too much. These are rules to describe phonetic changes in languages. I'll quote the original auth...

replace multiple values+in SQL query?

I want to do searching for data using users' entry in a field. That is if user enters "D+t+y+g,k,j,h" want to search for values having letters "d and t and y and g or k or j or h". I tried the PHP str_replace function, but didn't like the result. //kw is text field... if($kw != "") { //here we check for some data in field; if yes, c...

How do I parse a string representing a nested list into an actual list?

Say I have a string representing some nested lists and I want to convert it into the real thing. I could do this, I think: exec "myList = ['foo', ['cat', ['ant', 'bee'], 'dog'], 'bar', 'baz']" But in an environment where users might be supplying the string to execute this could/would be a bad idea. Does anybody have any ideas for a ti...

What is the most elegant way of converting the string 'a.pdf' to 'a.jpg' ?

I would like my code to look beautiful Thanks in advance.. ...

List enumeration in SQL?

Say, there are 2 tables: Person | id | name | LanguagesSpoken | person_id | language_name | Is there a way to create a view that would contain 2 columns: person_name and languages_spoken as a comma-separated list? I'm developing against SQLite. ...

Regular Expression String Omit Question

I'm trying to parse some AS3 with the use of Regular Expressions. I cannot, for the life of me, figure out how to omit matches that are inside of string quotations. I need to match test in the variable name testString, but not the test thats between the quotations. I don't want to match anything that's part of any string's content. var ...

Parsing a custom string-generating-pattern syntax

Background: I'm developing a custom regex-like syntax for URL filenames. It will work like this: User writes a pattern, something like "[a-z][0-9]{0,2}", and passes it as input It is parsed by the program and translated into the set of permutations it represents i.e. 'a', 'a0', 'a00' ... 'z99' These patterns will vary in complexity, ...

Unicode to string conversion in Java

I am building a language, a toy language. The syntax \#0061 is supposed to convert the given Unicode to an character: String temp = yytext().subtring(2); Then after that try to append '\u' to the string, I noticed that generated an error. I also tried to "\\" + "u" + temp; this way does not do any conversion. I am basically trying...

PHP substring extraction. Get the string before the first '/' or the whole string

I am trying to extract a substring. I need some help with doing it in PHP. Here are some sample strings I am working with and the results I need: home/cat1/subcat2 => home test/cat2 => test startpage => startpage I want to get the string till the first /, but if no / is present, get the whole string. I tried, substr($mystring, 0...

Regular Expression to split on specific character ONLY if that character is not in a pair

After finding the fastest string replace algorithm in this thread, I've been trying to modify one of them to suit my needs, particularly this one by gnibbler. I will explain the problem again here, and what issue I am having. Say I have a string that looks like this: str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog" You'...

Wide to narrow characters

What is the cleanest way of converting a std::wstring into a std::string? I have used W2A et al macros in the past, but I have never liked them. ...

need solution for split cell value (string) and view this as link in VIEW in ASP.NET MVC 1 project

In my table i have: id, header, centent and foto columns. In foto column are cells included string values for egxample :(foto1.jpg,foto2.jpg). J split this and the result input to table. I'm trying to view this in Details.aspx. I must view one record from table in my database plus one split cell as links. View the hole record is not the ...

Sql select query with where from multiple columns

I have a simple table CREATE TABLE a( id int IDENTITY(1,1) NOT NULL, x varchar(50) ) I found that following query works select cast (id as varchar(3))+cast (x as varchar(3)) c from a where cast (id as varchar(3))+cast (x as varchar(3))='1a' but this does not work select cast (id as varchar(3))+cast (x as varchar(3)) c...