我已使用PDFBox将PDF文档转换为文本。但是,许多单词用破折号分成2行。
例如,单词Others
更改为Oth-ers
,单词becoming
更改为be-coming
等。
如果我使用string.Replace
将“ - ”替换为空字符串,则会将单词之间的空格分开。
C#中是否有一种方法可以删除单词中间的短划线并将单词的各个部分再连接成一个单词?
答案 0 :(得分:2)
我写了这个单元测试,并且正确删除了短划线。
[TestMethod]
public void ReplaceDashByEmptyString()
{
string othersWithDash = "Oth-ers";
string othersWithoutDash = othersWithDash.Replace("-", string.Empty);
Assert.AreEqual("Others", othersWithoutDash);
}
答案 1 :(得分:-1)
您可以使用Regex.Replace
。
var text =@"Others Oth-ers";
var matches = Regex.Matches(text, @"\w+\-\w+");
foreach(Match dashMatch in matches)
{
text = Regex.Replace(text, Regex.Escape(dashMatch.Value), dashMatch.Value.Replace("-", string.Empty));
}
//Then you can have your new test variable