str-replace

can you write a str.replace() using dictionary values in Python?

我的未来我决定 提交于 2019-11-28 11:54:15
I have to replace the north, south, etc with N S in address fields. If I have list = {'NORTH':'N','SOUTH':'S','EAST':'E','WEST':'W'} address = "123 north anywhere street" Can I for iterate over my dictionary values to replace my address field? for dir in list[]: address.upper().replace(key,value) I know i'm not even close!! But any input would be appreciated if you can use dictionary values like this. address = "123 north anywhere street" for word, initial in {"NORTH":"N", "SOUTH":"S" }.items(): address = address.replace(word.lower(), initial) print address nice and concise and readable too.

How to skip first regex match?

吃可爱长大的小学妹 提交于 2019-11-28 10:59:37
问题 Is there anyway to skip the first match when using regex and php. Or is there some way of achieveing this using str_replace. Thanks UPDATE I am trying to remove all the instances of a string from another string but I want to retain the first occurance e.g $toRemove = 'test'; $string = 'This is a test string to test to removing the word test'; Ouput string would be: This is a test string to test to removing the word test 回答1: Easy PHP way: <?php $pattern = "/an/i"; $text = "banANA"; preg_match

Simple template var replacement, but with a twist

我是研究僧i 提交于 2019-11-28 10:27:56
问题 So I'm setting up a system that has a lot of emails, and variable replacement within it, so I'm writing a class to manage some variable replacement for templates stored in the database. Here's a brief example: // template is stored in db, so that's how this would get loaded in $template = "Hello, %customer_name%, thank you for contacting %website_name%"; // The array of replacements is built manually and passed to the class // with actual values being called from db $replacements = array('

swap two words in a string php

岁酱吖の 提交于 2019-11-28 09:58:39
Suppose there's a string "foo boo foo boo" I want to replace all fooes with boo and booes with foo. Expected output is "boo foo boo foo". What I get is "foo foo foo foo". How to get expected output rather than current one? $a = "foo boo foo boo"; echo "$a\n"; $b = str_replace(array("foo", "boo"), array("boo", "foo"), $a); echo "$b\n"; //expected: "boo foo boo foo" //outputs "foo foo foo foo" Use strtr From the manual: If given two arguments, the second should be an array in the form array('from' => 'to', ...). The return value is a string where all the occurrences of the array keys have been

Android Bug? : String.substring(5).replace(“”, “”) // empty string

家住魔仙堡 提交于 2019-11-28 09:33:54
Here is my code: String str = "just_a_string"; System.out.println("]" + str + "["); System.out.println("]" + str.replace("", "") + "["); System.out.println("]" + str.substring(5) + "["); System.out.println("]" + str.substring(5).replace("", "") + "["); System.out.println("]" + str.substring(3, 8) + "["); System.out.println("]" + str.substring(3, 8).replace("", "") + "["); System.out.println("]" + "sdajndan".substring(5).replace("", "") + "["); and here is the output 05-09 19:09:20.570: I/System.out(23801): ]just_a_string[ 05-09 19:09:20.570: I/System.out(23801): ]just_a_string[ 05-09 19:09:20

str_replace in Twig

我是研究僧i 提交于 2019-11-28 07:10:06
I want to do a simple str_replace in my twig template. I'm new to twig and probably I need to add new filter or sth like that or to use existing. How can I do this? Where can I find list of filters available? M Khalid Junaid To replace a string which is stored in twig variables: {% set twig_content_variable= 'Testing to replace content'%} {% set replace_value_var= 'Testing' %} {% set replace_with_value_var = 'Testing complete' %} {{ twig_content_variable|replace({ (replace_value_var): replace_with_value_var }) }} websky Use this to replace | with - and replace , width . : {{age|replace({'|': "

Can't remove special characters with str_replace

泄露秘密 提交于 2019-11-28 02:06:01
I have a very trivial problem with str_replace. I have a string with the En Dash character ( - ) like this: I want to remove - the dash The html output is I want to remove the – the dash I want to do this: $new_string = str_replace ('-','',$string); I've tried to parse the string with html_entity_decode, to parse the character to remove with htmlspecialchars,but without any results. What I'm doing wrong? -EDIT- This is the full code of my script: $title = 'Super Mario Galaxy 2 - Debut Trailer'; // Fetched from the DB, in the DB the character is - (minus) not – $new_title = str_replace(' - ', '

is there a way to use tr/// (or equivalent) in java?

柔情痞子 提交于 2019-11-28 01:49:36
I would like to know if there is an equivalent to tr/// (as used in Perl) in Java. For example, if I wanted to replace all "s"s with "p"s in "mississippi" and vice versa, I could, in Perl, write #shebang and pragmas snipped... my $str = "mississippi"; $str =~ tr/sp/ps/; # $str = "mippippissi" print $str; The only way I can think of to do it in Java is to use a dummy character with the String.replace() method, i.e. String str = "mississippi"; str = str.replace('s', '#'); // # is just a dummy character to make sure // any original 's' doesn't get switched to a 'p' // and back to an 's' with the

php str_ireplace without losing case

不羁的心 提交于 2019-11-28 01:37:05
问题 is it possible to run str_ireplace without it destroying the original casing? For instance: $txt = "Hello How Are You"; $a = "are"; $h = "hello"; $txt = str_ireplace($a, "<span style='background-color:#EEEE00'>".$a."</span>", $txt); $txt = str_ireplace($h, "<span style='background-color:#EEEE00'>".$h."</span>", $txt); this all works fine, but the result outputs: [hello] How [are] You instead of: [Hello] How [Are] You (square brackets being the color background) Thanks. 回答1: You're probably

Make text between asterisks bold

人走茶凉 提交于 2019-11-28 01:08:24
I wanted to make a PHP function that would make text bold between double asterisks, and italic between one asterisk, (quite like the editor on stackoverflow) . Same rules apply, if there's a space between the * and the word, it shouldn't render. Who can help me out? I tried to, but I only came this far, as I don't know how to make the odd asterisks "< b >" and the even ones "< /b >". (I can't type them without the spaces, stackoverflow will render the text between as bold.....) $thenewtext = str_replace("**", "<b>", "**Hello World** of PHP"); A simple regex will do the trick: $thenewtext =