Python strip with \n [duplicate]

You should be able to use line.strip(‘\n’) and line.strip(‘\t’). But these don’t modify the line variable…they just return the string with the \n and \t stripped. So you’ll have to do something like line = line.strip(‘\n’) line = line.strip(‘\t’) That should work for removing from the start and end. If you have \n and \t … Read more

How to strip all whitespace from string

Taking advantage of str.split’s behavior with no sep parameter: >>> s = ” \t foo \n bar ” >>> “”.join(s.split()) ‘foobar’ If you just want to remove spaces instead of all whitespace: >>> s.replace(” “, “”) ‘\tfoo\nbar’ Premature optimization Even though efficiency isn’t the primary goal—writing clear code is—here are some initial timings: $ python … Read more

Remove Characters from URL with htaccess

The URI is url-decoded before it’s sent through the rewrite engine, so you want to match the actual characters and not their encoded counterparts: RewriteRule ^(.*),(.*)$ /$1$2 [L] RewriteRule ^(.*):(.*)$ /$1$2 [L] RewriteRule ^(.*)\'(.*)$ /$1$2 [L] RewriteRule ^(.*)\”(.*)$ /$1$2 [L] # etc… RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301] The redirect status lets mod rewrite … Read more

How to remove whitespace from right end of NSString?

UPDATE: A quick benchmark showed that Matt’s own adaption, based on Max’ & mine, performs best. @implementation NSString (TrimmingAdditions) – (NSString *)stringByTrimmingLeadingCharactersInSet:(NSCharacterSet *)characterSet { NSUInteger location = 0; NSUInteger length = [self length]; unichar charBuffer[length]; [self getCharacters:charBuffer]; for (location; location < length; location++) { if (![characterSet characterIsMember:charBuffer[location]]) { break; } } return [self substringWithRange:NSMakeRange(location, length … Read more

How to strip a specific word from a string?

Use str.replace. >>> papa.replace(‘papa’, ”) ‘ is a good man’ >>> app.replace(‘papa’, ”) ‘app is important’ Alternatively use re and use regular expressions. This will allow the removal of leading/trailing spaces. >>> import re >>> papa=”papa is a good man” >>> app = ‘app is important’ >>> papa3 = ‘papa is a papa, and papa’ … Read more

How to remove whitespaces and newlines from every value in a JSON file?

Now I want to strip off all he whitespaces and newlines for every value in the JSON file Using pkgutil.simplegeneric() to create a helper function get_items(): import json import sys from pkgutil import simplegeneric @simplegeneric def get_items(obj): while False: # no items, a scalar object yield None @get_items.register(dict) def _(obj): return obj.items() # json object. … Read more

How to strip all non alphanumeric characters from a string in c++?

Write a function that takes a char and returns true if you want to remove that character or false if you want to keep it: bool my_predicate(char c); Then use the std::remove_if algorithm to remove the unwanted characters from the string: std::string s = “my data”; s.erase(std::remove_if(s.begin(), s.end(), my_predicate), s.end()); Depending on your requirements, you … Read more