Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
String Manipulator
Author:
Zvonimir Begic
E-mail:
Click to e-mail author
Submitted:
10/26/2011
Version:
VB 2010
Compatibility:
VB 2010
Category:
String Manipulation
Views:
8974
This is powerfull class for string manipulation. It has advanced functions and subs for strings.
Declarations:
Dim a as new StringManipulator(yourstring)
Code:
Imports System.Text Public Class StringManipulator Public Shared INSERT As Integer = 0 Public Shared REPLACE As Integer = 1 'main string object Private str As String 'arraylist for perm. Private arr As New ArrayList 'for returnStringSimiliarity(add or not to temp arraylist) Private addarraylistitem As Boolean = False Sub New(ByVal str As String) Me.str = str End Sub '''
''' Every word begin with uppercase letter(example: this is word-->This Is Word) '''
'''
'''
Function EveryFirstLetterInWordUpperCase() As String Dim s As New StringBuilder Dim ar() As String = str.Split(" ") For Each st As String In ar Dim sp As String = st If sp.Length > 0 Then Dim zn As Char = sp.Substring(0, 1) zn = Char.ToUpper(zn) sp = sp.Remove(0, 1) sp = sp.Insert(0, zn.ToString) End If s.Append(sp + " ") Next str = s.ToString.Trim Return s.ToString.Trim End Function '''
''' reverse string(example: abcd-->dcba) '''
'''
'''
Function returnReverseString() As String Dim s As New StringBuilder If str.Length > 0 Then For a As Integer = str.Length - 1 To 0 Step -1 s.Append(str.Substring(a, 1)) Next End If Return s.ToString End Function '''
''' returns array of substring starting position indexes '''
'''
string to find '''
arraylist of indexes
'''
Function returnAllIndexesOf(ByVal wantedSubstring As String) As ArrayList Dim ar As New ArrayList If str.Length > 0 Then Dim ls As Integer = str.IndexOf(wantedSubstring) Dim curpos As Integer = ls + wantedSubstring.Length While True If ls <> -1 Then ar.Add(ls) ls = str.IndexOf(wantedSubstring, curpos) curpos = ls + wantedSubstring.Length Else Exit While End If End While End If Return ar End Function '''
''' does string contains html link '''
'''
true-have html link, false-dont have html link
'''
Function haveHtmlLink() As Boolean If str.ToLower.Contains("http://") Or str.ToLower.Contains("https://") Then Return True Else Return False End If End Function '''
''' does string contains only numbers '''
'''
'''
Function haveOnlyNumbers() As Boolean Dim l As Boolean = True For Each c As Char In str If Char.IsNumber(c) = False Then l = False Exit For End If Next Return l End Function '''
''' if word is palindrome return true, else false(palindrome example: abba) '''
'''
'''
Function isPalindrome() As Boolean If str.Length > 1 Then Dim f As Boolean = True Dim nv As String = str.Replace(" ", "") Dim k As Integer = nv.Length - 1 For a As Integer = 0 To k If a = k Then Exit For End If If nv.ToLower.Substring(a, 1) <> nv.ToLower.Substring(k, 1) Then f = False Exit For End If k -= 1 Next Return f Else Return False End If End Function '''
''' does string have only ascii characters '''
'''
'''
Function haveOnlyAsciiCharacters() As Boolean Dim f As Boolean = True For Each c As Char In str If Asc(c) > 127 Then f = False Exit For End If Next Return f End Function '''
''' return logest word in the string '''
'''
'''
Function returnLongestWord() As String Dim st As String = "" Dim sp() As String = st.Split(" ") For Each s As String In sp If s.Length > st.Length Then st = s End If Next Return st End Function '''
''' return shortest word in the string '''
'''
'''
Function returnShortestWord() As String Dim st As String = str Dim sp() As String = st.Split(" ") For Each s As String In sp If s.Length < st.Length Then If s.Length > 0 Then st = s End If End If Next Return st End Function '''
''' counts word appereance in string '''
'''
'''
Function countEachWordAppearance() As Hashtable Dim h As New Hashtable Dim ar() As String = str.Split(" ") For Each s As String In ar If String.IsNullOrEmpty(s) = False Then If h.ContainsKey(s) = False Then h.Add(s, 1) Else h(s) = h(s) + 1 End If End If Next Return h End Function '''
''' counts letter appereance in string '''
'''
'''
Function countEachLetterAppearance() As Hashtable Dim h As New Hashtable For Each c As String In str If h.ContainsKey(c) = False Then h.Add(c, 1) Else h(c) = h(c) + 1 End If Next Return h End Function Private Sub permuting(ByRef chars() As Char, ByRef a As Integer, ByRef b As Integer) If a = b - 1 Then saveString(chars) Else For i As Integer = a To b - 1 Dim t As Char = chars(i) chars(i) = chars(a) chars(a) = t permuting(chars, a + 1, b) chars(a) = chars(i) chars(i) = t Next End If End Sub Private Sub saveString(ByVal chars() As Char) Dim st As New StringBuilder() For j As Integer = 0 To chars.Length - 1 st.Append(chars(j).ToString()) Next arr.Add(st.ToString) End Sub '''
''' return all string permutations ''' (example:for string abc we are getting abc,acb,bac,bca,cba,cab) '''
'''
'''
Function returnAllStringPermutations() As ArrayList permuting(str.ToCharArray, 0, str.Length) Return arr End Function '''
''' insert or replace desired string at specified locations ''' (example: abcdagf-->multiInsertOrReplace("z",StringManipulator.Replace,0,4) will give zbcdzgf) ''' (example: abcdagf-->multiInsertOrReplace("z",StringManipulator.Insert,0,4) will give zabcdzagf) '''
'''
'''
'''
'''
'''
Function multiInsertOrReplace(ByVal replaceString As String, ByVal stringOption As Integer, ByVal ParamArray stringplace() As Integer) As String Dim n As New StringBuilder Dim fr As Integer = 0 Dim zd As Integer = 0 For Each i As Integer In stringplace zd = i n.Append(str.Substring(fr, i - fr)) n.Append(replaceString) If fr > str.Length - 1 Then fr = str.Length - 1 End If If stringOption = INSERT Then fr = i ElseIf stringOption = REPLACE Then fr = i + 1 End If Next n.Append(str.Substring(fr, str.Length - fr)) str = n.ToString Return str End Function '''
''' remove substring at desired occurence position '''
'''
'''
'''
if occurence exist returns string, if does not exist then return nothing
'''
Function removeSubstringAtOccurence(ByVal stringToRemove As String, ByVal whatOccurence As Integer) As String Dim sl As String = Nothing Try Dim arl As ArrayList = returnAllIndexesOf(stringToRemove) str = str.Remove(CInt(arl(whatOccurence - 1)), stringToRemove.Length) sl = str Catch ex As Exception End Try Return sl End Function '''
''' after each full stop,question mark and exclamation mark sets first letter uppercase ''' Used for correct spelling '''
'''
'''
Function setUpperCaseLetterAfterFullStopQuestionMarkExclamationMark() As String Dim stb As New StringBuilder Dim interp As Boolean = False For Each c As Char In str If interp = True Then stb.Append(Char.ToUpper(c)) interp = False Else stb.Append(c) End If If c = "!" Or c = "." Or c = "?" Then interp = True End If Next str = stb.ToString Return str End Function '''
''' return longest similiarity between two strings ''' (example: string "exampleses" and string "sample" ''' similiarity is "ample" ''' ) '''
'''
string to check '''
shortest substring length that would be considered for cheking '''
arraylist with string objects
'''
Function returnStringSimilarity(ByVal stringForSimiliarityCheck As String, ByVal shortestWordLengthToIncludeInSimiliarity As Integer) As ArrayList Dim arl As New ArrayList Dim shorterword As String = stringForSimiliarityCheck Dim lenword As String = str If str.Length < stringForSimiliarityCheck.Length Then lenword = stringForSimiliarityCheck shorterword = str End If Try For a As Integer = 0 To shorterword.Length - 1 For b As Integer = shortestWordLengthToIncludeInSimiliarity To (shorterword.Length - a) Dim sb As String = shorterword.Substring(a, b) If lenword.Contains(sb) = True Then Dim bn As Boolean = doesArrayListHasString(arl, sb) If bn = False Then addarraylistitem = True deleteNoNeededArrayListItems(arl, sb) If addarraylistitem = True Then arl.Add(sb) End If End If End If Next Next Catch ex As Exception MessageBox.Show(ex.Message) End Try Return arl End Function Private Function doesArrayListHasString(ByVal ls As ArrayList, ByVal searchedString As String) As Boolean Dim b As Boolean = False For Each s As String In ls If s = searchedString Then b = True Exit For End If Next Return b End Function Private Sub deleteNoNeededArrayListItems(ByRef ls As ArrayList, ByVal searchedString As String) If ls.Count > 0 Then addarraylistitem = True Dim toind As Integer = ls.Count - 1 For a As Integer = 0 To toind If toind < a Then Exit For End If If ls(a).ToString.Contains(searchedString) Then addarraylistitem = False Exit For End If If searchedString.Contains(ls(a).ToString) Then ls.RemoveAt(a) toind -= 1 a -= 1 End If Next End If End Sub '''
''' extract and return numbers in arraylist ''' use this when you have predefined string format ''' (example: read financial .txt file for 2011 year ''' filename 2011.txt ''' month average total ''' 1 2854,32 3543.32 ''' 2 2345,78 4321,23 ''' etc. ''' ''' ) ''' You will get arraylist with items ''' 1 ''' 2854,32 ''' 3543.32 ''' 2 ''' 2345,78 ''' 4321,23 ''' ''' Then you know that you have 3 columns, ''' ar(0) is month,ar(1) is average, ar(2) is total ''' ar(3) is month,ar(4) is average, ar(5) is total '''
'''
'''
Function extractNumbersFromString() As ArrayList Dim arl As New ArrayList Dim strb As New StringBuilder Dim bc As Boolean = False Dim readNumber As Boolean = True Dim frs As Boolean = True For Each c As Char In str If frs = True Then If Char.IsWhiteSpace(c) = False Then readNumber = False End If frs = False End If If Char.IsDigit(c) = True Then If readNumber = True Then bc = True End If Else If c = "." Or c = "," Then bc = True readNumber = True Else bc = False If String.IsNullOrEmpty(strb.ToString) = False And readNumber = True Then Dim number As String = strb.ToString If Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator = "," Then number = number.Replace(".", ",") ElseIf Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator = "." Then number = number.Replace(",", ".") End If arl.Add(CDbl(number)) End If strb = New StringBuilder If Char.IsWhiteSpace(c) = True Then readNumber = True Else readNumber = False End If End If End If If bc = True Then strb.Append(c) End If Next If String.IsNullOrEmpty(strb.ToString) = False Then Dim number As String = strb.ToString If Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator = "," Then number = number.Replace(".", ",") ElseIf Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator = "." Then number = number.Replace(",", ".") End If arl.Add(CDbl(number)) End If Return arl End Function '''
''' count substring appereance in string '''
'''
'''
'''
Function getSubstringOccuranceCount(ByVal substr As String) As Integer Dim br As Integer = -1 Try Dim ind As Integer = 0 Dim dulj As Integer = 0 While ind > -1 ind = str.IndexOf(substr, ind + dulj) dulj = substr.Length If ind > -1 Then br += 1 End If End While br += 1 Catch ex As Exception MessageBox.Show(ex.Message) End Try Return br End Function '''
''' for making fixed size string ''' (example: string "abc" we want to make 5 char long with empty space ''' makeFixedSizeString(" ",5) and we get "abc " ''' ) '''
'''
'''
must be greater then current string size '''
Sub makeFixedSizeString(ByVal charForFillEndOfString As Char, ByVal newSizeOfString As Integer) Dim strb As New StringBuilder strb.Append(str) For a As Integer = str.Length To newSizeOfString strb.Append(charForFillEndOfString) Next str = strb.ToString End Sub End Class
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement