Csharp/C Sharp/Development Class/StringBuilder — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:45, 26 мая 2010
Содержание
- 1 Create a StringBuilder which hold 100 characters.
- 2 illustrates reading and writing string data
- 3 Length and Indexer
- 4 Replace char in a StringBuilder object
- 5 Replace Char with String
- 6 String Concatenation
- 7 Use StringBuilder to reverse a string
- 8 using the StringBuilder methods Replace, Insert, Append, AppendFormat, and Remove:
Create a StringBuilder which hold 100 characters.
using System;
using System.Text;
class StringApp {
static void Main(string[] args) {
StringBuilder myBuffer = new StringBuilder("My string data");
Console.WriteLine(myBuffer.Capacity);
myBuffer.Append(" contains some numerical data: ");
myBuffer.AppendFormat("{0}, {1}.", 44, 99);
Console.WriteLine(myBuffer.Capacity);
Console.WriteLine(myBuffer);
}
}
illustrates reading and writing string data
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example15_17.cs illustrates reading and writing string data
*/
using System;
using System.IO;
using System.Text;
public class Example15_17
{
public static void Main()
{
// create a new string to work with
StringBuilder sb = new StringBuilder();
// use a StringWriter to write data to the string
StringWriter sw = new StringWriter(sb);
// write some text to the string
sw.Write("This is a test of the StringWriter class");
sw.Close();
// now open the string for reading
StringReader sr = new StringReader(sb.ToString());
// read the entire string into a buffer and display it
string EntireString;
EntireString = sr.ReadToEnd();
Console.WriteLine(EntireString);
// clean up
sr.Close();
}
}
Length and Indexer
using System;
using System.Text;
class MainClass
{
public static string ReverseString(string str)
{
if (str == null || str.Length <= 1)
{
return str;
}
StringBuilder revStr = new StringBuilder(str.Length);
for (int count = str.Length - 1; count > -1; count--)
{
revStr.Append(str[count]);
}
return revStr.ToString();
}
public static void Main(){
Console.WriteLine(ReverseString("Madam Im Adam"));
Console.WriteLine(ReverseString("The quick brown fox jumped over the lazy dog."));
}
}
Replace char in a StringBuilder object
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
StringBuilder greetingBuilder = new StringBuilder("www.nfex.ru");
for (int i = (int)"z"; i >= (int)"a"; i--) {
char old1 = (char)i;
char new1 = (char)(i + 1);
greetingBuilder = greetingBuilder.Replace(old1, new1);
}
for (int i = (int)"Z"; i >= (int)"A"; i--) {
char old1 = (char)i;
char new1 = (char)(i + 1);
greetingBuilder = greetingBuilder.Replace(old1, new1);
}
Console.WriteLine("Encoded:\n" + greetingBuilder.ToString());
}
}
Replace Char with String
using System;
using System.Text;
public class MainClass {
static String ReplaceCharString(String s, char c1, String s2) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < s.Length; i++)
if (s[i] == c1)
res.Append(s2);
else
res.Append(s[i]);
return res.ToString();
}
}
String Concatenation
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
using System.Text;
namespace Client.Chapter_6___Strings
{
public class StringConcatenation
{
static void Main(string[] args)
{
StringBuilder MyString = new
StringBuilder("Hello");
MyString.Insert(0, "My");
MyString.Append("World");
Console.WriteLine(MyString);
}
}
}
Use StringBuilder to reverse a string
using System;
using System.Text;
class MainClass {
public static string ReverseString(string str)
{
if (str == null || str.Length <= 1)
{
return str;
}
StringBuilder revStr = new StringBuilder(str.Length);
for (int count = str.Length - 1; count > -1; count--)
{
revStr.Append(str[count]);
}
return revStr.ToString();
}
public static void Main() {
Console.WriteLine(ReverseString("The quick brown fox jumped over the lazy dog."));
}
}
using the StringBuilder methods Replace, Insert, Append, AppendFormat, and Remove:
using System;
using System.Text;
class UseSBApp {
static void Main(string[] args) {
StringBuilder sb = new StringBuilder("Pineapple");
sb.Replace("e", "X");
sb.Insert(4, "Banana");
sb.Append("Kiwi");
sb.AppendFormat(", {0}:{1}", 123, 45.6789);
sb.Remove(sb.Length - 3, 3);
Console.WriteLine(sb);
}
}