(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
String PadLeft and PadRight
using System;
class MainClass {
public static void Main() {
string str = "test";
Console.WriteLine("Original string: " + str);
// Pad on left with spaces.
str = str.PadLeft(10);
Console.WriteLine("|" + str + "|");
// Pad on right with spaces.
str = str.PadRight(20);
Console.WriteLine("|" + str + "|");
}
}
Original string: test
| test|
| test |
String: PadLeft/Right with character
using System;
class MainClass {
public static void Main() {
string str = " test ";
Console.WriteLine("Original string:" + str+"<");
// Pad on left with #s.
str = str.PadLeft(10, "#");
Console.WriteLine("|" + str + "|");
// Pad on right with #s.
str = str.PadRight(20, "#");
Console.WriteLine("|" + str + "|");
}
}
Original string: test <
| test |
| test #######|
String: Pad on right with #s
using System;
class MainClass {
public static void Main() {
string str = " test ";
Console.WriteLine("Original string:" + str+"<");
// Pad on right with #s.
str = str.PadRight(20, "#");
Console.WriteLine("|" + str + "|");
// Trim #s.
str = str.Trim("#");
Console.WriteLine("|" + str + "|");
}
}
Original string: test <
| test #######|
| test |
String: trim
using System;
class MainClass {
public static void Main() {
string str = " test ";
Console.WriteLine("Original string:" + str+"<");
// Trim spaces.
str = str.Trim();
Console.WriteLine("|" + str + "|");
}
}
Original string: test <
|test|
String: Trim #s
using System;
class MainClass {
public static void Main() {
string str = " test ";
Console.WriteLine("Original string:" + str+"<");
// Pad on right with #s.
str = str.PadRight(20, "#");
Console.WriteLine("|" + str + "|");
// Trim #s.
str = str.Trim("#");
Console.WriteLine("|" + str + "|");
}
}
Original string: test <
| test #######|
| test |
Trimming a string: trim start, trim end and trim chars
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
public class MainClass
{
public static void Main()
{
string s1 = " My ";
Console.WriteLine(s1);
Console.WriteLine(s1.Trim());
string s2 = "__...,, r!,..._";
Console.WriteLine(s2);
char[] trimChars = new char[] { ".", ",", "_" };
Console.WriteLine(s2.Trim(trimChars));
Console.WriteLine(s2.TrimStart(trimChars));
Console.WriteLine(s2.TrimEnd(trimChars));
}
}
My
My
__...,, r!,..._
r!
r!,..._
__...,, r!