Csharp/C Sharp/Data Types/String Split
Содержание
Char String foreach
using System;
public class StringSplitForeach
{
public static void Main()
{
string s = "Oh, I hadn"t thought of that";
char[] separators = new char[] {" ", ","};
foreach (string sub in s.Split(separators))
{
Console.WriteLine("Word: {0}", sub);
}
}
}
Show string in proper case
using System;
using System.Text;
public class StringEx
{
public static string ProperCase(string s)
{
s = s.ToLower();
string sProper = "";
char[] seps = new char[]{" "};
foreach (string ss in s.Split(seps))
{
sProper += char.ToUpper(ss[0]);
sProper +=
(ss.Substring(1, ss.Length - 1) + " ");
}
return sProper;
}
}
class MainClass
{
static void Main(string[] args)
{
string s = "tHE test";
Console.WriteLine("Initial String:\t{0}", s);
string t = StringEx.ProperCase(s);
Console.WriteLine("ProperCase:\t{0}", t);
}
}
Split and join strings
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Split and join strings.
using System;
public class SplitAndJoinDemo {
public static void Main() {
string str = "One if by land, two if by sea.";
char[] seps = {" ", ".", "," };
// Split the string into parts.
string[] parts = str.Split(seps);
Console.WriteLine("Pieces from split: ");
for(int i=0; i < parts.Length; i++)
Console.WriteLine(parts[i]);
// Now, join the parts.
string whole = String.Join(" | ", parts);
Console.WriteLine("Result of join: ");
Console.WriteLine(whole);
}
}
Split spaces with space
using System;
using System.Text;
class SplitStringApp {
static void Main(string[] args) {
string u = "A B A C E F";
char[] sep3 = new char[] { " " };
foreach (string ss in u.Split(sep3))
Console.WriteLine(ss);
}
}
Splitting Strings
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_6___Strings
{
public class SplittingStrings
{
static void Main(string[] args)
{
string MyString = "The quick brown fox ran around!";
string[] MyStringSplit = new string[6];
MyStringSplit = MyString.Split(new char[] { " " }, 6);
Console.WriteLine(MyStringSplit[1] + MyStringSplit[3]);
}
}
}
Split with a list of dividers
using System;
using System.Text;
class SplitStringApp {
static void Main(string[] args) {
string t = "Once,Upon:A/Time\\In\"America";
char[] sep2 = new char[]{" ", ",", ":", "/", "\\", "\""};
foreach (string ss in t.Split(sep2))
Console.WriteLine(ss);
}
}
Split with space
using System;
using System.Text;
class SplitStringApp
{
static void Main(string[] args)
{
string s = "Once Upon A Time In America";
char[] seps = new char[]{" "};
foreach (string ss in s.Split(seps))
Console.WriteLine(ss);
}
}
Tokenize strings
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Tokenize strings.
using System;
public class TokenizeDemo {
public static void Main() {
string[] input = {
"100 + 19",
"100 / 3.3",
"-3 * 9",
"100 - 87"
};
char[] seps = {" "};
for(int i=0; i < input.Length; i++) {
// split string into parts
string[] parts = input[i].Split(seps);
Console.Write("Command: ");
for(int j=0; j < parts.Length; j++)
Console.Write(parts[j] + " ");
Console.Write(", Result: ");
double n = Double.Parse(parts[0]);
double n2 = Double.Parse(parts[2]);
switch(parts[1]) {
case "+":
Console.WriteLine(n + n2);
break;
case "-":
Console.WriteLine(n - n2);
break;
case "*":
Console.WriteLine(n * n2);
break;
case "/":
Console.WriteLine(n / n2);
break;
}
}
}
}
use the Split() method to split strings
using System;
class MainClass {
public static void Main() {
string[] myStrings = {"To", "be", "or", "not","to", "be"};
string myString9 = String.Join(".", myStrings);
myStrings = myString9.Split(".");
foreach (string mySplitString in myStrings) {
Console.WriteLine("mySplitString = " + mySplitString);
}
}
}