UPDATE
Here is what I came up with. I have not tested it yet, because it is part of a much larger piece of code that still needs to be ported.
Can you see something that looks out of place?
private const string tempUserBlock = "%%%COMPRESS~USER{0}~{1}%%%";
string html = "some html";
int p = 0;
var userBlock = new ArrayList();
MatchCollection matcher = preservePatterns[p].Matches(html);
int index = 0;
StringBuilder sb = new StringBuilder();
int lastValue = 0;
foreach(Match match in matcher){
string matchValue = match.Groups[0].Value;
if(matchValue.Trim().Length > 0) {
userBlock.Add(matchValue);
int curIndex = lastValue + match.Index;
sb.Append(html.Substring(lastValue, curIndex));
sb.AppendFormat(tempUserBlock, p, index++);
lastValue = curIndex + match.Length;
}
}
sb.Append(html.Substring(lastValue));
html = sb.ToString();
ORIGINAL MAIL BELOW:
Here is the original Java:
private static final String tempUserBlock = "%%%COMPRESS~USER{0}~{1}%%%";
String html = "some html";
int p = 0;
List<String> userBlock = new ArrayList<String>();
Matcher matcher = patternToMatch.matcher(html);
int index = 0;
StringBuffer sb = new StringBuffer();
while (matcher.find())
{
if (matcher.group(0).trim().length() > 0)
{
userBlock.add(matcher.group(0));
matcher.appendReplacement(sb, MessageFormat.format(tempUserBlock, p, index++));
}
}
matcher.appendTail(sb);
html = sb.toString();
And my C # conversion is still
private const string tempUserBlock = "%%%COMPRESS~USER{0}~{1}%%%";
string html = "some html";
int p = 0;
var userBlock = new ArrayList();
MatchCollection matcher = preservePattern.Matches(html);
int index = 0;
StringBuilder sb = new StringBuilder();
for(var i = 0; i < matcher.Count; ++i){
string match = matcher[i].Groups[0].Value;
if(match.Trim().Length > 0) {
userBlock.Add(match);
sb.Append( string.Format(tempUserBlock, p, index++) );
}
}
matcher.appendTail(sb);
html = sb.toString();
See the comment above where I ask: “WHAT TO DO HERE?”
Explanation
The above Java code replaces a string with some HTML. It saves the original replaced text because it needs to be reinserted later after some compression of the space has been completed.
source
share