Csharp/C Sharp/Data Types/String Replace
Версия от 15:31, 26 мая 2010; (обсуждение)
Содержание
Inserting, replacing, and removing
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Inserting, replacing, and removing.
using System;
public class InsRepRevDemo {
public static void Main() {
string str = "This test";
Console.WriteLine("Original string: " + str);
// Insert
str = str.Insert(5, "is a ");
Console.WriteLine(str);
// Replace string
str = str.Replace("is", "was");
Console.WriteLine(str);
// Replace characters
str = str.Replace("a", "X");
Console.WriteLine(str);
// Remove
str = str.Remove(4, 5);
Console.WriteLine(str);
}
}
remove any of a set of chars from a given string.
using System;
public class Class1 {
public static void Main(string[] strings) {
char[] cSpecialChars = { "\n", ",", "_" };
string s = "_This is, a str\ning";
Console.WriteLine(RemoveSpecialChars(s, cSpecialChars));
}
public static string RemoveSpecialChars(string sInput,char[] cTargets) {
string sOutput = sInput;
foreach (char c in cTargets) {
for (; ; ) {
int nOffset = sOutput.IndexOf(c);
if (nOffset == -1) {
break;
}
string sBefore = sOutput.Substring(0, nOffset);
string sAfter = sOutput.Substring(nOffset + 1);
sOutput = sBefore + sAfter;
}
}
return sOutput;
}
}
Replace char inside a string
using System;
public class TestStringsApp {
public static void Main(string[] args) {
string a = "strong";
// Replace all "o" with "i"
string b = a.Replace("o", "i");
Console.WriteLine(b);
string c = b.Insert(3, "engthen");
string d = c.ToUpper();
Console.WriteLine(d);
}
}
String insert and output
using System;
public class StringTest
{
public static void Main()
{
string test1 = "This is a test string";
string test2, test3;
test2 = test1.Insert(15, "application ");
test3 = test1.ToUpper();
Console.WriteLine("test1: "{0}"", test1);
Console.WriteLine("test2: "{0}"", test2);
Console.WriteLine("test3: "{0}"", test3);
if (test1 == test3)
Console.WriteLine("test1 is equal to test3");
else
Console.WriteLine("test1 is not equal to test3");
test2 = test1.Replace("test", "sample");
Console.WriteLine("the new test2: "{0}"", test2);
}
}
String reverse and replace
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
using System;
// Declare a delegate.
delegate void strMod(ref string str);
public class StringOps {
// Replaces spaces with hyphens.
static void replaceSpaces(ref string a) {
Console.WriteLine("Replaces spaces with hyphens.");
a = a.Replace(" ", "-");
}
// Remove spaces.
static void removeSpaces(ref string a) {
string temp = "";
int i;
Console.WriteLine("Removing spaces.");
for(i=0; i < a.Length; i++)
if(a[i] != " ") temp += a[i];
a = temp;
}
// Reverse a string.
static void reverse(ref string a) {
string temp = "";
int i, j;
Console.WriteLine("Reversing string.");
for(j=0, i=a.Length-1; i >= 0; i--, j++)
temp += a[i];
a = temp;
}
public static void Main() {
// Construct delegates.
strMod strOp;
strMod replaceSp = new strMod(replaceSpaces);
strMod removeSp = new strMod(removeSpaces);
strMod reverseStr = new strMod(reverse);
string str = "This is a test";
// Set up multicast.
strOp = replaceSp;
strOp += reverseStr;
// Call multicast.
strOp(ref str);
Console.WriteLine("Resulting string: " + str);
Console.WriteLine();
// Remove replace and add remove.
strOp -= replaceSp;
strOp += removeSp;
str = "This is a test."; // reset string
// Call multicast.
strOp(ref str);
Console.WriteLine("Resulting string: " + str);
Console.WriteLine();
}
}
use the Insert(), Remove(), and Replace() methods to modify strings
using System;
class MainClass {
public static void Main() {
string[] myStrings = {"To", "be", "or", "not","to", "be"};
string myString = String.Join(".", myStrings);
string myString10 = myString.Insert(6, "friends, ");
Console.WriteLine("myString.Insert(6,\"friends, \") = " + myString10);
string myString11 = myString10.Remove(14, 7);
Console.WriteLine("myString10.Remove(14, 7) = " + myString11);
string myString12 = myString11.Replace(",", "?");
Console.WriteLine("myString11.Replace(",", "?") = " + myString12);
string myString13 =myString12.Replace("to be", "Or not to be friends");
Console.WriteLine("myString12.Replace(\"to be\",\"Or not to be friends\") = " + myString13);
}
}