CodeDocu.com

C#: Split string into words

 

In c#, you can split a long text into individual words, by one string-text using the method string. Split(..) in an array converts with string values.

string[] arrWords = sText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries );

 

Then, you can iterate through the string array and remove the individual values.

By the parameter of StringSplitOptions. RemoveEmptyEntries prevents null values in the array to be written.

 

The following c# code example shows how a filter text into a search box for Web pages is divided into a filter string.

This string is the SQL-where condition that filters the SQL fields.

//--< Split Text to Words >--

string[] arrWords = sText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries );

for (int iWord = 0; iWord < arrWords.Length; iWord++)

{

    string sWord = arrWords[iWord];

    sFilter = sFilter + " AND [Path] LIKE '%" + sWord + "%'";

}

 

Example in the Photo Explorer, where various texts be sought on the basis of combined words separated by a space.

Will SQL condition with AND linked.

C#: Split String into single Words String-Array

 

 

Complete c# split example code with reconciliation in SQL string a SQLite database

string sFilter = "";

 

//--< Split Text to Words >--

string[] arrWords = sText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries );

for (int iWord = 0; iWord < arrWords.Length; iWord++)

{

    string sWord = arrWords[iWord];

    sFilter = sFilter + " AND [Path] LIKE '%" + sWord + "%'";

}

 

 

//< replace 1 >

System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(" AND ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

sFilter = regEx.Replace(sFilter, " ",1, 0);

//</ replace 1 >

//--</ Split Text to Words >--

 

 

 

//< get records >

string sSQL = @"SELECT [Foldername], [Path] FROM Folders WHERE " + sFilter  + " ORDER BY Path";

ISQLiteStatement recFolders = clsDB.get_Recordset(sSQL);

//</ get records >

 

 

 

 


Software Entwicklung Stuttgart NĂ¼rtingen
Suche Projekte C#, WPF, Windows App,ASP.Net, vb.Net, WinForms, SQL Server, Access, Excel