str-replace

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

时光毁灭记忆、已成空白 提交于 2019-11-27 06:34:43
问题 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. 回答1: address = "123 north anywhere street" for word, initial in {"NORTH":"N", "SOUTH":"S" }

How to replace multiple values in php

一世执手 提交于 2019-11-27 05:29:41
$srting = "test1 test1 test2 test2 test2 test1 test1 test2"; How can I change test1 values to test2 and test2 values to test1 ? When I use str_replace and preg_replace all values are changed to the last array value. Example: $pat = array(); $pat[0] = "/test1/"; $pat[1] = "/test2/"; $rep = array(); $rep[0] = "test2"; $rep[1] = "test1"; $replace = preg_replace($pat,$rep,$srting) ; Result: test1 test1 test1 test1 test1 test1 test1 test1 Rizier123 This should work for you: <?php $string = "test1 test1 test2 test2 test2 test1 test1 test2"; echo $string . "<br />"; echo $string = strtr($string,

How to Replace dot (.) in a string in Java

孤者浪人 提交于 2019-11-27 04:56:51
I have a String called persons.name I want to replace the DOT . with /*/ i.e my output will be persons/*/name I tried this code: String a="\\*\\"; str=xpath.replaceAll("\\.", a); I am getting StringIndexOutOfBoundsException. How do I replace the dot? Femi You need two backslashes before the dot, one to escape the slash so it gets through, and the other to escape the dot so it becomes literal. Forward slashes and asterisk are treated literal. str=xpath.replaceAll("\\.", "/*/"); //replaces a literal . with /*/ http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang

swap two words in a string php

回眸只為那壹抹淺笑 提交于 2019-11-27 03:19:19
问题 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" 回答1: Use strtr From the manual: If given two arguments, the second should be an array in the form

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

假如想象 提交于 2019-11-27 03:00:05
问题 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

Replace multiple placeholders with PHP?

百般思念 提交于 2019-11-27 02:28:09
问题 I have a function that sends out site emails (using phpmailer), what I want to do is basically for php to replace all the placheholders in the email.tpl file with content that I feed it. The problem for me is I don't want to be repeating code hence why I created a function (below). Without a php function I would do the following in a script // email template file $email_template = "email.tpl"; // Get contact form template from file $message = file_get_contents($email_template); // Replace

str_replace in Twig

↘锁芯ラ 提交于 2019-11-27 01:45:01
问题 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? 回答1: 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

Str_replace for multiple items

狂风中的少年 提交于 2019-11-27 00:11:03
I remember doing this before, but can't find the code. I use str_replace to replace one character like this: str_replace(':', ' ', $string); but I want to replace all the following characters \/:*?"<>| , without doing a str_replace for each. str_replace() can take an array, so you could do: $new_str = str_replace(str_split('\\/:*?"<>|'), ' ', $string); Alternatively you could use preg_replace() : $new_str = preg_replace('~[\\\\/:*?"<>|]~', ' ', $string); Dogbert Like this: str_replace(array(':', '\\', '/', '*'), ' ', $string); Or, in modern PHP (anything from 5.4 onwards), the slighty less

Make text between asterisks bold

[亡魂溺海] 提交于 2019-11-26 23:28:54
问题 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 =

Can't remove special characters with str_replace

点点圈 提交于 2019-11-26 22:06:27
问题 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