/*
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);
}
}
Month: February 2011
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);
}
}
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);
}
}
Formats a string to the current culture.
//---------------------------------------------------------------------
// This file is part of the Background Motion solution.
//
// Copyright (C) Mindscape (TM). All rights reserved.
// http://www.mindscape.co.nz
//
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//---------------------------------------------------------------------
using System.Globalization;
namespace Mindscape.BackgroundMotion.Core.Utilities
{
/// <summary>
/// A helper class which provides utilities for working with strings
/// </summary>
public static class StringUtils
{
/// <summary>
/// Formats a string to the current culture.
/// </summary>
/// <param name="formatString">The format string.</param>
/// <param name="objects">The objects.</param>
/// <returns></returns>
public static string FormatCurrentCulture(string formatString, params object[] objects)
{
return string.Format(CultureInfo.CurrentCulture, formatString, objects);
}
}
}
Formats a string to an invariant culture
//---------------------------------------------------------------------
// This file is part of the Background Motion solution.
//
// Copyright (C) Mindscape (TM). All rights reserved.
// http://www.mindscape.co.nz
//
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//---------------------------------------------------------------------
using System.Globalization;
namespace Mindscape.BackgroundMotion.Core.Utilities
{
/// <summary>
/// A helper class which provides utilities for working with strings
/// </summary>
public static class StringUtils
{
/// <summary>
/// Formats a string to an invariant culture
/// </summary>
/// <param name="formatString">The format string.</param>
/// <param name="objects">The objects.</param>
/// <returns></returns>
public static string FormatInvariant(string formatString, params object[] objects)
{
return string.Format(CultureInfo.InvariantCulture, formatString, objects);
}
/// <summary>
/// Formats a string to the current culture.
/// </summary>
/// <param name="formatString">The format string.</param>
/// <param name="objects">The objects.</param>
/// <returns></returns>
public static string FormatCurrentCulture(string formatString, params object[] objects)
{
return string.Format(CultureInfo.CurrentCulture, formatString, objects);
}
}
}
Format with {0:F}
using System;
using System.Globalization;
using System.Text;
public class NumParsingApp {
public static void Main(string[] args) {
string u = "AA -1,234,567.890 ";
NumberFormatInfo ni = new NumberFormatInfo();
ni.CurrencySymbol = "AA";
double h = Double.Parse(u, NumberStyles.Any, ni);
Console.WriteLine("h = {0:F}", h);
}
}

