str-replace

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

半城伤御伤魂 提交于 2019-11-26 22:00:41
问题 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

C# String Replace

人盡茶涼 提交于 2019-11-26 21:29:30
问题 I want to replace "," with a ; in my string. For Example: Change this "Text","Text","Text", to this "Text;Text;Text", I've been trying the line.replace( ... , ... ) but can't get anything working properly. Any help would be appreciated. 回答1: Have you tried this: line.Replace("\",\"", ";") 回答2: The simplest way is to do line.Replace(@",", @";"); Output is shown as below: 回答3: You need to escape the double-quotes inside the search string, like this: string orig = "\"Text\",\"Text\",\"Text\"";

When to use strtr vs str_replace?

一世执手 提交于 2019-11-26 19:42:42
I'm having a hard time understanding when strtr would be preferable to str_replace or vice versa. It seems that it's possible to achieve the exact same results using either function, although the order in which substrings are replaced is reversed. For example: echo strtr('test string', 'st', 'XY')."\n"; echo strtr('test string', array( 's' => 'X', 't' => 'Y', 'st' => 'Z' ))."\n"; echo str_replace(array('s', 't', 'st'), array('X', 'Y', 'Z'), 'test string')."\n"; echo str_replace(array('st', 't', 's'), array('Z', 'Y', 'X'), 'test string'); This outputs YeXY XYring YeZ Zring YeXY XYring YeZ Zring

How to replace multiple items from a text string in PHP? [duplicate]

寵の児 提交于 2019-11-26 19:41:21
问题 This question already has answers here : How do I replace certain parts of my string? (5 answers) Closed 3 years ago . I want to be able to replace spaces with - but also want to remove commas and question marks. How can I do this in one function? So far, I have it replacing spaces: str_replace(" ","-",$title) 回答1: You can pass arrays as parameters to str_replace() . Check the manual. // Provides: You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables

PHP preg_replace/preg_match vs PHP str_replace

倖福魔咒の 提交于 2019-11-26 19:18:57
问题 Can anyone give me a quick summary of the differences please? To my mind they both do the same thing? Thanks 回答1: str_replace replaces a specific occurrence of a string, for instance "foo" will only match and replace that: "foo". preg_replace will do regular expression matching, for instance "/f.{2}/" will match and replace "foo", but also "fey", "fir", "fox", "f12", etc. [EDIT] See for yourself: $string = "foo fighters"; $str_replace = str_replace('foo','bar',$string); $preg_replace = preg

String.replaceAll() is not working

做~自己de王妃 提交于 2019-11-26 16:51:05
问题 I am editing some email that got from tesseract ocr. Here is my code: if (email != null) { email = email.replaceAll(" ", ""); email = email.replaceAll("caneer", "career"); email = email.replaceAll("canaer", "career"); email = email.replaceAll("canear", "career"); email = email.replaceAll("caraer", "career"); email = email.replaceAll("carear", "career"); email = email.replace("|", "l"); email = email.replaceAll("}", "j"); email = email.replaceAll("j3b", "job"); email = email.replaceAll("gmaii

How can I perform a str_replace in JavaScript, replacing text in JavaScript?

百般思念 提交于 2019-11-26 15:29:35
问题 I want to use str_replace or its similar alternative to replace some text in JavaScript. var text = "this is some sample text that i want to replace"; var new_text = replace_in_javascript("want", "dont want", text); document.write("new_text"); should give this is some sample text that i dont want to replace If you are going to regex, what are the performance implications in comparison to the built in replacement methods. 回答1: Using regex for string replacement is significantly slower than

Trying to replace parts of string start with same search chars

蹲街弑〆低调 提交于 2019-11-26 14:58:47
问题 I'm trying to replace parts of my string. But I met a problem when my search string start with same character: $string = "Good one :y. Keep going :y2"; $str = str_replace(array_keys($my_array), array_values($my_array), $string); $my_array= array(":y" => "a", ":y2" => "b"); ouput: Good one a. Keep going a2 I need my str_replace() to match the word correctly/exactly. 回答1: Besides that you should define your array first before you use it, this should work for you: $str = strtr($string, $my_array

How to replace multiple values in php

点点圈 提交于 2019-11-26 11:36:22
问题 $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 回答1: This should work for you: <?php $string =

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

核能气质少年 提交于 2019-11-26 11:19:34
问题 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? 回答1: 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("\\.", "/*/"); /