我写了一个将小写字符替换为 * 的代码。但它不起作用。问题出在哪儿?
private void CharacterReplacement()
{
Console.WriteLine("Enter a string to replacement : ");
string TargetString = Console.ReadLine();
string MainString = TargetString;
for (int i = 0; i < TargetString.Length; i++)
{
if (char.IsLower(TargetString[i]))
{
TargetString.Replace(TargetString[i], '*');
}
}
Console.WriteLine("The string {0} has converted to {1}", MainString, TargetString);
}
Run Code Online (Sandbox Code Playgroud) 我有一个用户可以随时编辑的字符串,以及一个正在对该字符串进行的正则表达式,将其添加到 xml 中,然后保存它,但他们可以将 '$1' 添加到字符串中。我只想保存文本“$1”,但我必须对 $1 所在的同一个字符串执行正则表达式。它每次都用正则表达式中的一个字符替换 $1。
如何查找并替换此字符串中的 $1?
正在发生的事情的例子:
string1 = '<item id="1">i have $100</item>'
regexp = new RegExp('<item id="1"([^<]|<[^\/]|<\/[^i]|<\/i[^t]|<\/it[^e]|<\/ite[^m]|<\/item[^>])*<\/item>');
data = '<data><item id="1">i have no money</item><item id="2">i have no money</item></data>'
data = data.replace(regexp, string1);
Run Code Online (Sandbox Code Playgroud)
结果
<data><item id="1">i have >00</item><item id="2">i have no money</item></data>
Run Code Online (Sandbox Code Playgroud) 我想用 html<br />标签替换字符串中的空格,最好的方法是什么?目前我有这个代码用“-”替换空格。
$(".msn").each(function() {
$(this).text($(this).text().replace(/ /g, '-'));
});
Run Code Online (Sandbox Code Playgroud) 因此,该程序运行良好,但即使合并了 abarnert 建议的最后修改后,它仍然无法确保生成唯一的突变。
这是我到目前为止所得到的。我确定是不对的,但是我并不完全理解python是如何执行下面abarnert编写的代码的。
a = open (scgenome, 'r')
codon = [ ]
for line in a:
data=line.split("\t")
codon.append(data[12])
import random
def string_replace(s,index,char):
return s[:index] + char + s[index+1:]
for x in range(1,1000):
index = random.randrange(3)
letter_to_replace = random.choice(list({"A", "G", "T", "C"} - {codon[index]}))
mutated_codon = [string_replace(codon[x], index, letter_to_replace)]
for c in mutated_codon:
codon_lookup[c]
Run Code Online (Sandbox Code Playgroud)
我也尝试在不使用范围的情况下以您的方式编写此代码,尽管我喜欢使用范围函数以便我可以打印出 10 或 100 个密码子并手动检查输出是否正确,但随后我得到了一个 Keyerror: 'r',在尝试确保每个替换都是唯一的之前,当我运行这个程序时没有发生过:
def string_replace(s,index,char):
return s[:index] + char + s[index+1:]
def mutate_codon(codon):
index = random.randrange(3)
letter_to_replace = random.choice(list({"A", "G", "T", "C"} …Run Code Online (Sandbox Code Playgroud) 最近在使用 arrayList 时,我发现了一个奇怪的问题。我想在数组列表中保留 2 个元素,并希望在元素 0 中保留 object1,在 element1 中保留 object2。
我在循环中决定这个。
当它碰巧添加第一个元素时,它会抛出 indexOutofBounds。根据java doc,因为索引大于大小,所以这样做。
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add(1,"SECONDITEM"); // throwing due to size is not set
list.add(0,"FIRSTITEM");
int postn=0;
for(String item:list){
System.out.println("Position "+postn+" :"+item);
postn++;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试将其他项目设置为 0,1 元素中的占位符并尝试做同样的事情
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add(0,"x");
list.add(1,"y");
list.add(1,"SECONDITEM");
list.add(0,"FIRSTITEM");
int postn=0;
for(String item:list){
System.out.println("Position "+postn+" :"+item);
postn++;
}
}
Output:
Position 0 :FIRSTITEM
Position 1 …Run Code Online (Sandbox Code Playgroud) 我已经用过str_replace很多角色,而且效果很好。对于我的生活,它不适用于双引号。它根本不做任何事情:
$value = str_replace('"', '', $value);
Run Code Online (Sandbox Code Playgroud)
这不会删除双引号。是否有一些 Apache 或 PHP 设置阻止它工作?
编辑:trim($value, '"')也不起作用!我想我快疯了。
编辑 2:AbraCadaver 明白了!谢谢它需要“
抱歉,如果之前已经问过这个问题。我没有通过搜索找到答案。需要在 Python 中替换文件名的子字符串。
旧字符串:“_ready”
新字符串:“_busy”
文件:a_ready.txt、b_ready.txt、c.txt、d_blala.txt、e_ready.txt
输出:a_busy.txt、b_busy.txt、c.txt、d_blala.txt、e_busy.txt
有任何想法吗?我尝试使用 replce(),但没有任何反应。这些文件仍然使用旧名称。
这是我的代码:
import os
counter = 0
for file in os.listdir("c:\\test"):
if file.endswith(".txt"):
if file.find("_ready") > 0:
counter = counter + 1
print ("old name:" + file)
file.replace("_ready", "_busy")
print ("new name:" + file)
if counter == 0:
print("No file has been found")
Run Code Online (Sandbox Code Playgroud) 所以我试图构建一个完全动态的日历网络应用程序,只是为了一些练习,但我正在努力动态替换这个 H1 元素文本。
我已经将所有月份都应用于创造性命名的“month”变量,并且输出正确,但我似乎无法替换我的占位符
<h1 id="replace">Month</h1>
Run Code Online (Sandbox Code Playgroud)
使用
function title(){
var myReplace = document.getElementById("replace");
myReplace.innerHTML = month;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码使用此处给出的详细信息替换字符串的一部分:
#include <iostream>
#include <string>
using namespace std ;
int main(){
string ori = "this is a test"; // to replace "is a" with "IS A"
string part = "is a";
int posn = ori.find(part);
int len = part.size();
cout << posn << endl;
ori.replace(posn, len, ori, "IS A");
cout << ori;
}
Run Code Online (Sandbox Code Playgroud)
但是,它给出一个很长的错误,开始于:
rnreplacestr.cpp:11:36: error: no matching function for call to ‘std::__cxx11::basic_string<char>::replace(int&, int&, std::__cxx11::string&, const char [5])’
ori.replace(posn, len, ori, "IS A");
^
In file included from …Run Code Online (Sandbox Code Playgroud) 我有一个表列,它有像这样的行:
Joe Loe
korea china
Dan Ger
Run Code Online (Sandbox Code Playgroud)
Dan和Ger之间有两个空格。我想替换在列中包含多个空格的该列值,但是我该怎么做。请帮助我。
SELECT myTable.Columnx
FROM table myTable
where REGEXP_LIKE(myTable.Columnx, '[[:space:]]*','')
Run Code Online (Sandbox Code Playgroud)