//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers. //CruiseControl is distributed under a BSD-style license. //http://cruisecontrol.sourceforge.net/ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace ThoughtWorks.CruiseControl.Core.Util { /// <summary> /// Class with handy stirng routines /// </summary> public class StringUtil { /// <summary> /// returns the elements of the array as a string, delimited with the default delimitor /// </summary> /// <param name="x"></param> /// <returns></returns> public static string GetArrayContents(Array x) { System.Text.StringBuilder result = new System.Text.StringBuilder(); string DEFAULT_DELIMITER = ","; foreach (object o in x) { result.AppendFormat("{0}{1} ", o.ToString(), DEFAULT_DELIMITER); } if (result.Length > 0) { result.Length -= 2; } return result.ToString(); } } }
Data Types
New Line Separated String To Array
//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers. //CruiseControl is distributed under a BSD-style license. //http://cruisecontrol.sourceforge.net/ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace ThoughtWorks.CruiseControl.Core.Util { /// <summary> /// Class with handy stirng routines /// </summary> public class StringUtil { public static string[] NewLineSeparatedStringToArray(string input) { if (string.IsNullOrEmpty(input)) return new string[0]; List<string> targets = new List<string>(); using (StringReader reader = new StringReader(input)) { while (reader.Peek() >= 0) { targets.Add(reader.ReadLine()); } } return targets.ToArray(); } } }
Array To New Line Separated String
//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers. //CruiseControl is distributed under a BSD-style license. //http://cruisecontrol.sourceforge.net/ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace ThoughtWorks.CruiseControl.Core.Util { /// <summary> /// Class with handy stirng routines /// </summary> public class StringUtil { public static string ArrayToNewLineSeparatedString(string[] input) { StringBuilder sb = new StringBuilder(); foreach (string file in input) { if (sb.Length > 0) sb.Append(Environment.NewLine); sb.Append(file); } return sb.ToString(); } } }
Splits the string into an array, using the separator.
// // (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com) // All rights reserved. // /* Copyright (c) 2001-2008, The HSQL Development Group * All rights reserved. * * Redistribution and use _in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions _in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer _in the documentation * and/or other materials provided with the distribution. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ using System; using System.Text; using System.Collections.Generic; namespace EffiProz.Core.Lib { /** Provides a collection of convenience methods for processing and * creating objects with <code>String</code> value components. * * @author fredt@users * @author boucherb@users * @version 1.7.2 * @since 1.7.0 */ public class StringUtil { /** * Splits the string into an array, using the separator. If separator is * not found _in the string, the whole string is returned _in the array. * * @param s the string * @param separator the separator * @return array of strings */ public static String[] split(string s, string separator) { List<string> list = new List<string>(); int currindex = 0; for (bool more = true; more; ) { int nextindex = s.IndexOf(separator, currindex); if (nextindex == -1) { nextindex = s.Length; more = false; } list.Add(s.Substring(currindex, nextindex - (currindex))); currindex = nextindex + separator.Length; } return (String[])list.ToArray(); } } }
Gets an array of sentences from a string.
#region Copyright (c) 2004, Ryan Whitaker /********************************************************************************* ' ' Copyright (c) 2004 Ryan Whitaker ' ' This software is provided 'as-is', without any express or implied warranty. In no ' event will the authors be held liable for any damages arising from the use of this ' software. ' ' Permission is granted to anyone to use this software for any purpose, including ' commercial applications, and to alter it and redistribute it freely, subject to the ' following restrictions: ' ' 1. The origin of this software must not be misrepresented; you must not claim that ' you wrote the original software. If you use this software in a product, an ' acknowledgment (see the following) in the product documentation is required. ' ' This product uses software written by the developers of NClassifier ' (http://nclassifier.sourceforge.net). NClassifier is a .NET port of the Nick ' Lothian's Java text classification engine, Classifier4J ' (http://classifier4j.sourceforge.net). ' ' 2. Altered source versions must be plainly marked as such, and must not be ' misrepresented as being the original software. ' ' 3. This notice may not be removed or altered from any source distribution. ' '********************************************************************************/ #endregion using System; using System.Collections; using System.Text.RegularExpressions; namespace NClassifier { public class Utilities { /// <summary> /// Gets an array of sentences. /// </summary> /// <param name="input">A string that contains sentences.</param> /// <returns>An array of strings, each element containing a sentence.</returns> public static string[] GetSentences(string input) { if (input == null) return new string[0]; else { // split on a ".", a "!", a "?" followed by a space or EOL // the original Java regex was (.|!|?)+(s|z) string[] result = Regex.Split(input, @"(?:.|!|?)+(?:s+|z)"); // hacky... doing this to pass the unit tests ArrayList list = new ArrayList(); foreach (string s in result) if (s.Length > 0) list.Add(s); return (string[])list.ToArray(typeof(string)); } } } }
Find all unique words in an array of words.
#region Copyright (c) 2004, Ryan Whitaker
/*********************************************************************************
'
' Copyright (c) 2004 Ryan Whitaker
'
' This software is provided 'as-is', without any express or implied warranty. In no
' event will the authors be held liable for any damages arising from the use of this
' software.
'
' Permission is granted to anyone to use this software for any purpose, including
' commercial applications, and to alter it and redistribute it freely, subject to the
' following restrictions:
'
' 1. The origin of this software must not be misrepresented; you must not claim that
' you wrote the original software. If you use this software in a product, an
' acknowledgment (see the following) in the product documentation is required.
'
' This product uses software written by the developers of NClassifier
' (http://nclassifier.sourceforge.net). NClassifier is a .NET port of the Nick
' Lothian's Java text classification engine, Classifier4J
' (http://classifier4j.sourceforge.net).
'
' 2. Altered source versions must be plainly marked as such, and must not be
' misrepresented as being the original software.
'
' 3. This notice may not be removed or altered from any source distribution.
'
'********************************************************************************/
#endregion
using System;
using System.Collections;
using System.Text.RegularExpressions;
namespace NClassifier
{
public class Utilities
{
///
///
/// An array of strings.
///
public static string[] GetUniqueWords(string[] input)
{
if (input == null)
return new string[0];
else
{
ArrayList result = new ArrayList();
for (int i = 0; i < input.Length; i++)
if (!result.Contains(input[i]))
result.Add(input[i]);
return (string[])result.ToArray("".GetType());
}
}
}
}
[/csharp]
Count how many times a word appears in an array of words.
#region Copyright (c) 2004, Ryan Whitaker
/*********************************************************************************
'
' Copyright (c) 2004 Ryan Whitaker
'
' This software is provided 'as-is', without any express or implied warranty. In no
' event will the authors be held liable for any damages arising from the use of this
' software.
'
' Permission is granted to anyone to use this software for any purpose, including
' commercial applications, and to alter it and redistribute it freely, subject to the
' following restrictions:
'
' 1. The origin of this software must not be misrepresented; you must not claim that
' you wrote the original software. If you use this software in a product, an
' acknowledgment (see the following) in the product documentation is required.
'
' This product uses software written by the developers of NClassifier
' (http://nclassifier.sourceforge.net). NClassifier is a .NET port of the Nick
' Lothian's Java text classification engine, Classifier4J
' (http://classifier4j.sourceforge.net).
'
' 2. Altered source versions must be plainly marked as such, and must not be
' misrepresented as being the original software.
'
' 3. This notice may not be removed or altered from any source distribution.
'
'********************************************************************************/
#endregion
using System;
using System.Collections;
using System.Text.RegularExpressions;
namespace NClassifier
{
public class Utilities
{
///
///
/// The word to count.
/// A non-null array of words.
public static int CountWords(string word, string[] words)
{
// find the index of one of the items in the array
int itemIndex = Array.BinarySearch(words, word);
// iterate backwards until we find the first match
if (itemIndex > 0)
while (itemIndex > 0 && words[itemIndex] == word)
itemIndex–;
// now itemIndex is one item before the start of the words
int count = 0;
while (itemIndex < words.Length && itemIndex >= 0)
{
if (words[itemIndex] == word)
count++;
itemIndex++;
if (itemIndex < words.Length) if (words[itemIndex] != word) break; } return count; } } } [/csharp]