C# 用其他字符串替换多个令牌
我正在将FoxPro翻译成C,很难理解如何拆分变量。我的身体里有需要交换的单词,它们在~。例如,~this~。是否有一个C等同于FoxPro的At?代码如下:C# 用其他字符串替换多个令牌,c#,replace,C#,Replace,我正在将FoxPro翻译成C,很难理解如何拆分变量。我的身体里有需要交换的单词,它们在~。例如,~this~。是否有一个C等同于FoxPro的At?代码如下: lnxx = At("~",lc_body,1) If lnxx = 0 *--paste rest into new body lc_newbody = lc_newbody + lc_body Exit Endif
lnxx = At("~",lc_body,1)
If lnxx = 0
*--paste rest into new body
lc_newbody = lc_newbody + lc_body
Exit
Endif
*--get ending one
lnyy = At("~",lc_body,2)
*--get string out
lc_string = Substr(lc_body,lnxx,lnyy-lnxx+1)
*--remove ~
lc_string = Strtran(lc_string,"~","")
*--get part to ~, past in new one
If lnxx>1
lc_substring = Substr(lc_body,1,lnxx-1)
lc_newbody = lc_newbody + lc_substring
*-cut to second ~
lc_body = Substr(lc_body,lnyy+1,5000)
Else
*--it starts with ~, handle this case
***newbody is blank
lc_newbody = ""
*-cut to second ~
lc_body = Substr(lc_body,lnyy+1,5000)
Endif
将为您提供字符串中字符的位置。将分隔字符串拆分为一系列字符串。C可以在更少的空间内完成更多的工作。该语言中塞满了实用函数 事实上,我本人从未使用过Foxpro,但我认为我根据在网上找到的注释和文档理解了上述代码。正如@HighCore所建议的,惯用C将为您提供更多可读性和可维护性的代码 您所展示的这一特定部分可以简单地表示为: var parts=lc_body.Splitnew[]{'~'},3; 如果parts.Length==1 { lc_新车身+=lc_车身;
return;//您似乎遇到了一个问题 XY问题是询问您试图解决的问题,而不是实际问题 也就是说,你试图解决问题X,你认为解决方案Y是可行的,但当你遇到麻烦时,你不是问X,而是问Y 关于你手头的问题: 我的身体中有需要交换的单词,它们在~中。例如,~this~ 这与FoxPro中任何可用方法的解决方式无关 要替换另一个字符串值中的字符串值,只需使用 和代码:
using System;
public class Program
{
public static void Main()
{
var originalText = "{0} is a sentence that contains {0} more than once.";
Console.WriteLine(originalText);
var replacedText = string.Format(originalText, "~that~");
Console.WriteLine(replacedText);
}
}
using System;
public class Program
{
public static void Main()
{
var originalText = "{0} should be [this], and {2} should be [that], and there should be no [thus]";
Console.WriteLine(originalText);
var replacedText = string.Format(originalText, "this", "thus", "that");
Console.WriteLine(replacedText);
}
}
使用制度
public class Program
{
public static void Main()
{
var originalText = "~this~ is a sentence that contains ~this~ more than once.";
Console.WriteLine(originalText);
var replacedText = originalText.Replace("~this~", "~that~");
Console.WriteLine(replacedText);
}
}
结果:
~this~是一个多次包含~this~的句子
~that~是一个不止一次包含~that~的句子
如果您可以控制源文本,那么我强烈建议您使用
和代码:
using System;
public class Program
{
public static void Main()
{
var originalText = "{0} is a sentence that contains {0} more than once.";
Console.WriteLine(originalText);
var replacedText = string.Format(originalText, "~that~");
Console.WriteLine(replacedText);
}
}
using System;
public class Program
{
public static void Main()
{
var originalText = "{0} should be [this], and {2} should be [that], and there should be no [thus]";
Console.WriteLine(originalText);
var replacedText = string.Format(originalText, "this", "thus", "that");
Console.WriteLine(replacedText);
}
}
结果:
{0}是一个多次包含{0}的句子
~that~是一个不止一次包含~that~的句子
String.Format的优点在于,如果没有找到要替换的替换,那么就没有异常,它只是不替换任何内容。这解决了具有多个替换语句的问题,而只依赖于一个方法:
和代码:
using System;
public class Program
{
public static void Main()
{
var originalText = "{0} is a sentence that contains {0} more than once.";
Console.WriteLine(originalText);
var replacedText = string.Format(originalText, "~that~");
Console.WriteLine(replacedText);
}
}
using System;
public class Program
{
public static void Main()
{
var originalText = "{0} should be [this], and {2} should be [that], and there should be no [thus]";
Console.WriteLine(originalText);
var replacedText = string.Format(originalText, "this", "thus", "that");
Console.WriteLine(replacedText);
}
}
结果:
{0}应该是[这个],{2}应该是[那个],不应该有[这样]
这应该是[这个],那应该是[那个],不应该有[这样]
最好忘记这件可怕的事情,用惯用C语言重写,很可能你可以在一行/两行C代码中完成所有这些。你真正需要做什么?例如,不要迭代字符串并检查~你将使用myString.Split“~”;假设我的字符串是Hello,~CONTACT~。请回答。我需要联系人,以便替换它带有实际的联系人姓名。有时它可以是~*contact~,因此我也需要星号。在~I中的任何内容都需要替换并写入新的字符串消息。您正在进行令牌替换?例如,Hello~contact~变成Hello Bob Smith?myString.replace~contact~,Bob Smith;完成。如果更复杂,请使用Regex.replace.Repl使用Substring和IndexOf标记应该永远不会发生。Edit:您在C中寻找的函数是IndexOf,但有更好的方法来实现这一点。它并不总是联系的。我需要替换~then switch case中的内容,具体取决于单词是什么。请给出一个示例,说明这将如何与上面的代码一起工作?re@Erik Philips的响应也很好,但确实需要插入点看起来像{0},{1},等等。如果你想使用命名值,那么你必须做一些腿部工作,比如上面我编辑的答案,或者将命名插入点转换为string.Format可以使用的数字。请在dotnetfiddle.net中显示DoReplacements的工作示例,好吗?