我已经在这里研究了其他问题,但还没有找到适合我的问题.我正在尝试从文件中选择一个特定的行,并用另一个字符串替换该行上的字符串.所以我有一个名为my_course的文件.我正在尝试修改my_course中以"123"开头的一行.在那一行,我想用"1"替换字符串"0".救命?
好吧,假设我有一个这样的字符串:
String strExample = "name=\"bob\" age=\"25\" salary=\"$500 per month\"";
Run Code Online (Sandbox Code Playgroud)
我想摆脱''''字符('\"\"'之间)的任何东西.
我一直试图用这个:
String newStr = strExample.replaceAll("=\"[ ]*\"", "");
Run Code Online (Sandbox Code Playgroud)
问题是,我无法弄清楚要在'[]'部分放置什么来摆脱引用中的内容.我能得到的只有:
name
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能让newStr返回:
name age salary
Run Code Online (Sandbox Code Playgroud)
我不能使用XML解析器作为我正在做的事情的要求不允许我这样做.
我sed用来查找和替换文件中的一些项目,如下所示:
sed -i "s/$a/$b/g" ./file.txt
Run Code Online (Sandbox Code Playgroud)
我需要限制此查找并替换为仅当匹配文本位于方括号之间时,例如:
The sheep lived in the barn.
One day, [the sheep ate some] grass.
The [sheep] found a nice place to sleep under the tree.
Afterwards, [the sheep ate apples under] the tree [near the barn].
The sheep [enjoyed the theme] of the movie.
Run Code Online (Sandbox Code Playgroud)
假设$a设置为"the"并$b设置为"B",输出将如下所示:
The sheep lived in the barn.
One day, [B sheep ate some] grass.
The [sheep] found a nice place to sleep under the tree. …Run Code Online (Sandbox Code Playgroud) 我试图保存一些图像,我想使用DateTime来获得不同的可识别文件名.因此,我使用正确的Path创建一个String,向其添加日期时间并删除空格,点和冒号.
String imagePath = "D:\\Patienten\\" + username;
imagePath += "\\"+DateTime.Now.ToString();
Console.WriteLine("WithFilename: " + imagePath);
imagePath.Replace(" ", "");
Console.WriteLine("Without \" \" : " + imagePath);
imagePath.Replace(".", "");
Console.WriteLine("Without \".\": " + imagePath);
imagePath.Replace(":", "");
Console.WriteLine("Output format: " + imagePath);
imagePath += ".png";
image.Save(imagePath);
Run Code Online (Sandbox Code Playgroud)
根据控制台输出,String根本没有变化.这意味着Console.Writeline中的所有输出字符串都是相同的.我在visual Studio Express 2010中使用c#,以防万一.有人能在这里找到错误吗?
提前致谢!
我有一个想要替换的图像,但......
<div class="news-thumb-wrapper">
<img src="/content/oldimage.jpg" class="attachment-post-thumbnail" height="150" width="600">
<div class="gallery-arrows"></div><h8><a href="/">Some text</a></h8></div>
Run Code Online (Sandbox Code Playgroud)
我已经使用此代码尝试替换整个img标记但它似乎不起作用.
$( "img.attachment-post-thumbnail" ).replaceWith( "<img src="/content/newimage.png" class="attachment-post-thumbnail" height="41" width="600">" );
Run Code Online (Sandbox Code Playgroud)
谁能告诉我代码有什么问题?
我有一个像这样的字符串 - 2013年11月15日.我想用2000替换2013(最后2位数为0).这该怎么做?
如何替换下面的字符串.
String test = "This is my Test's cases";
Run Code Online (Sandbox Code Playgroud)
现在我用空格替换"'"就像这样"这是我的测试案例"我尝试过:
1) replace("'","")
2) replaceAll("'","")
3) replace("\'","")
Run Code Online (Sandbox Code Playgroud)
...但我没有得到任何必要的结果.
测试代码:
String test = "The First American's style";
System.out.println("old text::"+test);
test = test.replaceAll("'","\\'");
System.out.println("new text::"+test);
Run Code Online (Sandbox Code Playgroud) 如何使用javascript删除行尾的br标签?
<code>
line 1 <br>
line 2 <Br>
line 3 <br />
line 4 <br/>
</code>
Run Code Online (Sandbox Code Playgroud) 我想创建一个小程序,我可以使用扫描仪输入字符串,并替换字符.例如,字符串中的每个"a"都应替换为"4".
我有这个源代码:
Scanner s = new Scanner(System.in);
String string = s.nextLine();
System.out.println("Your old text:" + original_string);
string.replace("i", "1");
string.replace("a", "4");
System.out.println("Your new super awesome text: " + string);
Run Code Online (Sandbox Code Playgroud)
例如,如果我输入"ia",它应该返回"14".不幸的是,这不会发生.
我一直在研究一些Java代码,我遇到了一个非常奇怪的错误,我试图用Integer替换一段字符串.添加了各种调试但我似乎无法找到它.
private void executeMsg() {
value = value.replaceAll("(&([a-f0-9]))", "§$2");
String border = tostiuhc.getWorldManager().getBorderSize() - tostiuhc.getShrinksize() + "";
String time = tostiuhc.getPhase().getMinutes() + "";
System.out.println("BORDER: " + border);
System.out.println("TIME: " + time);
value = value.replace("-border-", border + "");
value = value.replace("-time-", time + "");
tostiuhc.broadcast(value + " " + time);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我创建了一个名为'time'的新String,并将其打印出来'TIME:value'.我正在改变的原始字符串是:该事件现在是时间 - 分钟!


这里的问题是System.out.println("TIME:")显示正确的值,但.replace只是DOESNT工作.我无法理解这一点,任何人都有任何想法?