Csharp/CSharp Tutorial/String/String Pad Trim — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:16, 26 мая 2010

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!