← Tutti gli articoli
Split Words - with c# classic function and Linq
31 August 2010 ·
C# · Article ·
88 visite
Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsSplitWords
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SplitWords sw = new SplitWords(textBox1.Text);
foreach (string str in sw.Split())
{
if (str.Trim() != "")
{
listBox1.Items.Add(str);
}
}
}
private void btnSpiltWithLinq_Click(object sender, EventArgs e)
{
char[] splitchars=new char[] { ' ', ',', '.', ';', ':', '\t' };
string textToSplit= textBox1.Text;
var words = from word in textToSplit.Split(splitchars)
select word;
foreach (var v in words)
{
listBox1.Items.Add(v);
}
}
}
public class SplitWords{
string _words="";
char[] _splitchars=new char[] { ' ', ',', '.', ';', ':', '\t' };
public SplitWords(string words)
{
_words=words;
}
public SplitWords(string words, char[] splitchars)
{
_words=words;
_splitchars=splitchars;
}
public string[] Split()
{
string [] split = _words.Split();
return split;
}
}
}
