本文整理汇总了C#中StringBuilder.Replace方法的典型用法代码示例。如果您正苦于以下问题:C# StringBuilder.Replace方法的具体用法?C# StringBuilder.Replace怎么用?C# StringBuilder.Replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringBuilder
的用法示例。
在下文中一共展示了StringBuilder.Replace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Replace
private static void Replace(StringBuilder str)
{
str.Replace("<a href=\"", "[URL=");
str.Replace("\">","]");
str.Replace("</a>", "[/URL]");
Console.WriteLine(str.ToString());
}
示例2: Main
static void Main()
{
StringBuilder hay = new StringBuilder();
for (int line = 0; line < 4; line++)
{
hay.Append(Console.ReadLine().Trim());
hay.Append("\r\n");
}
string small = @">{1}-{5}>{1}";
string medium = @">{2}-{5}>{1}";
string large = @">{3}-{5}>{2}";
int largeCount = Regex.Matches(hay.ToString(), large).Count;
hay.Replace(">>>----->>", " ");
int mediumCount = Regex.Matches(hay.ToString(), medium).Count;
hay.Replace(">>----->", " ");
int smallCount = Regex.Matches(hay.ToString(), small).Count;
string decimalCount = "" + smallCount + mediumCount + largeCount;
string binaryCount = Convert.ToString(int.Parse(decimalCount), 2);
string binaryReversed = string.Join("", binaryCount.ToCharArray().Reverse());
string binary = binaryCount + binaryReversed;
Console.WriteLine(Convert.ToInt32(binary, 2));
}
示例3: GetRelativePath
/// <summary>
/// Turns the given full path into a relative one starting just above the given directory.
/// For example, "GetRelativePath("C:/A/B/C", "B")" returns "B/C".
/// Returns "null" if the given folder can't be found.
/// </summary>
/// <remarks>
/// As a side effect, all '/' or '\' slashes will be changed
/// to the correct directory separator char for this platform.
/// </remarks>
/// <param name="startFolder">
/// The folder that will appear at the top of the returned path.
/// </param>
public static string GetRelativePath(string fullPath, string startFolder)
{
StringBuilder sb = new StringBuilder(fullPath);
if ('/' != Path.DirectorySeparatorChar)
{
sb.Replace('/', Path.DirectorySeparatorChar);
}
if ('\\' != Path.DirectorySeparatorChar)
{
sb.Replace('\\', Path.DirectorySeparatorChar);
}
//Get the start of the given folder in the path string.
int folderLoc = sb.ToString().IndexOf(Path.DirectorySeparatorChar +
startFolder +
Path.DirectorySeparatorChar);
if (folderLoc < 0 && sb.ToString().StartsWith(startFolder + Path.DirectorySeparatorChar))
{
folderLoc = 0;
}
//If the given folder was found, cut out everything before that.
if (folderLoc >= 0)
{
sb.Remove(0, folderLoc);
if (sb[0] == Path.DirectorySeparatorChar)
sb.Remove(0, 1);
return sb.ToString();
}
else
{
return null;
}
}
示例4: bt_Export_Click
protected void bt_Export_Click(object sender, EventArgs e)
{
BindGrid(true, false);
string filename = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(lb_ReportTitle.Text.Trim() == "" ? "Export" : lb_ReportTitle.Text.Trim()));
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
Page.EnableViewState = false;
StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
try
{
string tmp0 = "<tr align=\"left\" valign=\"middle\" OnMouseOver=\"this.style.cursor='hand';this.originalcolor=this.style.backgroundColor;this.style.backgroundColor='#FFCC66';\" OnMouseOut=\"this.style.backgroundColor=this.originalcolor;\" style=\"height:18px;\">";
string tmp1 = "<tr align=\"left\" valign=\"middle\" OnMouseOver=\"this.style.cursor='hand';this.originalcolor=this.style.backgroundColor;this.style.backgroundColor='#FFCC66';\" OnMouseOut=\"this.style.backgroundColor=this.originalcolor;\" style=\"background-color:White;height:18px;\">";
StringBuilder outhtml = new StringBuilder(tw.ToString());
outhtml = outhtml.Replace(tmp0, "<tr>");
outhtml = outhtml.Replace(tmp1, "<tr>");
outhtml = outhtml.Replace(" ", "");
Response.Write(outhtml.ToString());
}
catch
{
Response.Write(tw.ToString());
}
Response.End();
BindGrid(false, false);
}
示例5: Main
static void Main()
{
int n = int.Parse(Console.ReadLine());
int step = int.Parse(Console.ReadLine());
StringBuilder bits = new StringBuilder();
for (int i = 0; i < n; i++)
{
int num = int.Parse(Console.ReadLine());
string currentLineBits = Convert.ToString(num, 2).PadLeft(8, '0');
bits.Append(currentLineBits);
}
for (int i = 0; i < bits.Length; i+=step)
{
char currentDigit = bits[i];
if (currentDigit == '0')
{
bits.Replace('0', '1', i, 1);
}
else
{
bits.Replace('1', '0', i, 1);
}
}
for (int i = 0; i < bits.Length; i+=8)
{
string current = bits.ToString().Substring(i, 8);
int num = Convert.ToInt32(current, 2);
Console.WriteLine(num);
}
}
示例6: ExtractHTMLBody
static void ExtractHTMLBody(string input)
{
StringBuilder bodyText = new StringBuilder();
int textStart = 0;
for (int i = textStart; i < input.Length - 1; i++)
{
if (input[i] == '<' || (input[i] == '<' && input[i + 1] == '/'))
{
while (input[i] != '>')
{
i++;
}
}
else
{
bodyText.Append(input[i]);
}
if (input[i] != '>' && input[i + 1] == '<')
{
bodyText.Append("\n");
}
}
bodyText.Replace("\r\n", " ");
bodyText.Replace(" ", " ").Replace(" ", " ");
bodyText.Replace(" ", " ").Replace(" ", " ");
Console.WriteLine("{0}", bodyText);
}
示例7: Main
static void Main()
{
byte n = byte.Parse(Console.ReadLine());
StringBuilder carpet = new StringBuilder();
carpet.Capacity = n + 2;
carpet.Append('.', n/2 - 1);
carpet.Append("/\\");
carpet.Append('.', n/2 - 1);
Console.WriteLine(carpet);
for (byte i = 0; i < n/2 - 1; i++)
{
if((i & 1) == 0)
{
carpet.Insert(n/2, " ");
}
else
{
carpet.Insert(n/2, "/\\");
}
carpet.Remove(n + 1, 1);
carpet.Remove(0, 1);
Console.WriteLine(carpet);
}
carpet.Replace('/','\\', 0, n/2);
carpet.Replace('\\','/', n/2, n/2);
Console.WriteLine(carpet);
for (byte i = 0; i < n / 2 - 1; i++)
{
carpet.Remove(n / 2 - 1, 2);
carpet.Append('.', 1);
carpet.Insert(0, '.');
Console.WriteLine(carpet);
}
}
示例8: Main
static void Main()
{
StreamReader reader = new StreamReader(@"..\..\text.txt");
StringBuilder result = new StringBuilder();
using (reader)
{
string line = reader.ReadLine();
while ( line != null)
{
StringBuilder lineToResult = new StringBuilder();
lineToResult.Append(line);
int wordIndex = line.IndexOf("start");
if (wordIndex == -1)
{
line = reader.ReadLine();
continue;
}
else
{
while (wordIndex != -1)
{
if (IsPossibleIndex(wordIndex - 1, line.Length) && IsPossibleIndex(wordIndex + 5, line.Length))
{
if (char.IsLetter(line[wordIndex - 1]) == false && char.IsLetter(line[wordIndex + 5]) == false)
{
lineToResult.Replace("start", "finish", wordIndex, 7);
}
}
else if (IsPossibleIndex(wordIndex - 1, line.Length))
{
if (char.IsLetter(line[wordIndex - 1]) == false)
{
lineToResult.Replace("start", "finish", wordIndex, 6);
}
}
else if (IsPossibleIndex(wordIndex + 5, line.Length))
{
if (char.IsLetter(line[wordIndex + 5]) == false)
{
lineToResult.Replace("start", "finish", wordIndex, 6);
}
}
wordIndex = line.IndexOf("start", wordIndex + 4);
}
result.AppendLine(lineToResult.ToString());
}
line = reader.ReadLine();
}
StreamWriter writer = new StreamWriter(@"..\..\result.txt");
using (writer)
{
writer.Write(result.ToString());
}
}
}
示例9: Main
static void Main()
{
StringBuilder sb = new StringBuilder();
sb.Append(Console.ReadLine());
sb.Replace("-", "");
sb.Replace(",", "");
sb.Replace(".", "");
BigInteger sum = 0;
do
{
sum = 0;
for (int i = 0; i < sb.Length; i++)
{
sum += (BigInteger)Char.GetNumericValue(sb[i]);
}
sb.Clear();
sb.Append(sum);
} while (sum > 9);
Console.WriteLine(sum);
}
示例10: InjectSVGViewer
public static bool InjectSVGViewer(string SVGPath, string virtualRoot)
{
try
{
if (System.IO.File.Exists(SVGPath))
{
StringBuilder contents = null;
using (StreamReader sr = new StreamReader(SVGPath))
{
contents = new StringBuilder(sr.ReadToEnd());
}
System.IO.File.Delete(SVGPath);
string searchfor = "http://www.w3.org/1999/xlink\">";
contents.Replace(searchfor, "http://www.w3.org/1999/xlink\" onload=\"init(evt)\" >");
searchfor = "</svg>";
contents.Replace(searchfor, "<script xlink:href=\"" + virtualRoot + "/Scripts/SVGzoom.js\"/>\n<script xlink:href=\"" + virtualRoot + "/Scripts/effect.js\"/>\n" + searchfor);
using (StreamWriter sw = new StreamWriter(SVGPath, false, Encoding.Default, contents.Length))
{
sw.Write(contents.ToString());
sw.Close();
}
}
}
catch (Exception e)
{
return false;
}
return true;
}
示例11: Main
static void Main()
{
int numLines = int.Parse(Console.ReadLine());
string indentation = Console.ReadLine();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numLines; i++)
{
if (i > 0) sb.Append(Environment.NewLine);
sb.Append(Console.ReadLine());
}
sb.Replace("{", Environment.NewLine + "{" + Environment.NewLine);
sb.Replace("}", Environment.NewLine + "}" + Environment.NewLine);
int sbLen = 0;
do
{
sbLen = sb.Length;
sb.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
sb.Replace(" "," ");
} while (sbLen != sb.Length);
string[] lines = sb.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
string indent = "";
for (int i = 0; i < lines.Length; i++)
{
lines[i] = lines[i].Trim();
if (lines[i] == "") continue;
if (lines[i] == "}")
indent = indent.Remove(0, indentation.Length);
Console.Write(indent);
Console.WriteLine(lines[i]);
if (lines[i] == "{")
indent = indent + indentation;
}
}
示例12: FixDirectorySeparators
/// <summary>
/// Replaces all directory separators with the correct one for the current platform (either / or \).
/// </summary>
public static string FixDirectorySeparators(string path)
{
StringBuilder sb = new StringBuilder(path);
sb.Replace('/', Path.DirectorySeparatorChar);
sb.Replace('\\', Path.DirectorySeparatorChar);
return sb.ToString();
}
示例13: GetRelativePath
/// <summary>
/// Turns the given full path into a relative one starting just above the given directory.
/// For example, "GetRelativePath("C:/A/B/C", "B")" returns "B/C".
/// Returns "null" if the given folder can't be found.
/// </summary>
/// <remarks>
/// As a side effect, all '/' or '\' slashes will be changed
/// to the correct directory separator char for this platform.
/// </remarks>
/// <param name="startFolder">
/// The folder that will appear at the top of the returned path.
/// </param>
public static string GetRelativePath(string fullPath, string startFolder)
{
StringBuilder sb = new StringBuilder(fullPath);
if ('/' != Path.DirectorySeparatorChar)
{
sb.Replace('/', Path.DirectorySeparatorChar);
}
if ('\\' != Path.DirectorySeparatorChar)
{
sb.Replace('\\', Path.DirectorySeparatorChar);
}
int folderLoc = sb.ToString().IndexOf(Path.DirectorySeparatorChar +
startFolder +
Path.DirectorySeparatorChar);
if (folderLoc >= 0)
{
sb.Remove(0, folderLoc);
if (sb[0] == Path.DirectorySeparatorChar)
sb.Remove(0, 1);
return sb.ToString();
}
else
{
return null;
}
}
示例14: Main
static void Main()
{
int number = int.Parse(Console.ReadLine());
List<int> bitsPositions = new List<int>();
List<string> commands = new List<string>();
while (true)
{
string bitPos = Console.ReadLine();
if (bitPos == "quit")
{
break;
}
bitsPositions.Add(int.Parse(bitPos));
string command = Console.ReadLine();
commands.Add(command);
}
StringBuilder manipulate = new StringBuilder();
string numberAsBits = Convert.ToString(number, 2).PadLeft(32, '0');
manipulate.Append(numberAsBits);
for (int i = 0; i < commands.Count; i++)
{
string currentCommand = commands[i];
int pos = 31 - bitsPositions[i];
switch (currentCommand)
{
case "flip":
if (manipulate[pos] == '0')
{
manipulate.Replace('0', '1', pos, 1);
}
else
{
manipulate.Replace('1', '0', pos, 1);
}
break;
case "remove":
manipulate.Remove(pos, 1);
manipulate.Insert(0, '0');
break;
case "insert":
manipulate.Remove(0, 1);
manipulate.Insert(pos, '1');
break;
}
}
ulong result = Convert.ToUInt64(manipulate.ToString(), 2);
Console.WriteLine(result);
}
示例15: TagReplacer
private static void TagReplacer(string text)
{
StringBuilder builder = new StringBuilder(text);
builder.Replace("<a href=\"", "[URL=");
builder.Replace("\">", "]");
builder.Replace("</a>", "[/URL]");
Console.WriteLine(builder);
}