How can I convert a string into different characters? - vb.net

I have looked on the web and I cannot find anything that helps me, all I can find is changing the characters into ASCII or Hexadecimal. However I would like to do it a different way. For example, say the string that got passed in was abcdef, I would like to have a key which changes these characters into another string such as qwpolz. Is there an easier way than declaring each character in the alphabet to be another character like:
Dim sText As String = "Hello"
Dim sEncode As String = ""
Dim iLength As Integer
Dim i As Integer
iLength = Len(sText)
For i = 1 To iLength
sEncode = sEncode ????
Next
Return sEncode
And then have a very lengthy loop which checks for these loops? There must be a much simpler way. Can anybody help by pointing me in the right direction?
Edit: Why downvote? Seriously, it's a legitimate question. Instead of downvoting for no reason, just move onto another question.

Well actually, this sounds like a Caesar sipher
Private Overloads Shared Function Encrypt(ByVal ch As Char, ByVal code As Integer) As Char
If Not Char.IsLetter(ch) Then
Return ch
End If
Dim offset As Char = IIf(Char.IsUpper(ch), "A", "a")
Return CType((((ch + (code - offset)) Mod 26) + offset),Char)
End Function
Private Overloads Shared Function Encrypt(ByVal input As String, ByVal code As Integer) As String
Return New String(input.ToCharArray.Select(() => { }, Encrypt(ch, code)).ToArray)
End Function
Private Shared Function Decrypt(ByVal input As String, ByVal code As Integer) As String
Return Encrypt(input, (26 - code))
End Function
Note that this assumes, that you use English alphabet. In general case where for example you have 'ä', 'ö', 'š', 'ž', 'ß', 'õ', 'ü' etc. this would not work. In that case it is simpler to just create a list/dictionary of your ordered alphabet and use it.
Example use:
encrypted = Encrypt(sText, 5)
decypted = Decrypt(encrypted, 5)

Sounds as if you want to modify a string by replacing each character with a different character according to a mapping table. An efficient approach is to use a Dictionary(Of Char, Char). But easier to write and maintain is something like this:
Shared ReadOnly replaceChars As String = "abcdef"
Shared ReadOnly withChars As String = "qwpolz"
Public Shared Function ReplaceAll(input As String) As String
Dim newChars = From c In input
Let index = replaceChars.IndexOf(c)
Select If(index >= 0, withChars(index), c)
Return String.Concat(newChars)
End Function
So the first string contains the chars that you want to replace and the second the replacement characters. Both strings must have the same length.
If you want to support case insensitivity:
Public Shared Function ReplaceAll(input As String, comparison As StringComparison) As String
Dim newChars = From c In input
Let index = replaceChars.IndexOf(c.ToString(), comparison)
Select If(index >= 0, withChars(index), c)
Return String.Concat(newChars)
End Function
Note that this is also a loop. There is no way to avoid some kind of loops if you want to replace multiple characters or strings.

Related

VB.net regex exclude certain characters from string

I am using regex.ismatch to check a string doesn't contain any one of a list of characters such as £&+(/?!;:* And also a quotation mark " not sure how to place that...
But can't get to to work...
If Regex.ismatch(Line, "^[^##£&+()*']"). Then
Msgbox("error")
End If
But doesn't work for me?
Any suggestions
You could do this pretty easily without Regex by simply doing something like this:
Public Shared Function HasSpecialChars(ByVal str As String) As Boolean
Const specialChars As String = "!##$%^&*()"
Dim indexOf As Integer = str.IndexOfAny(specialChars.ToCharArray())
Return indexOf <> -1
End Function

Get the character after a combination of characters in a string

I have a string ABC(N9KGRTLMN9(0J)M3.
I want to return the character after GRTLM which is N. Thanks.
Look at the System.Text.RegularExpressions namespace, and create a RegEx object with this expression:
GRTLM(.)
Then you will be able to check the Matches for the expression to find your character. Depending on what you know about that string, you may be able to narrow things even further. For example:
GRTLM([A-Za-z])
or
GRTLM([A-Z])
If you don't want to use regular expressions (for any reason), here's an alternative:
Private Function ReturnCharAfter(Source As String, after As String) As Char
Dim i As Integer = Source.IndexOf(after)
If i < 0 Then Return Nothing
Return Source(i + after.Length)
End Function
usage:
Dim N As Char = ReturnCharAfter("ABC(N9KGRTLMN9(0J)M3.", "GRTLM")
You could use String.Split() to get the N
Dim input = "ABC(N9KGRTLMN9(0J)M3"
Dim s = "GRTLM"
Dim n = input.Split({s}, StringSplitOptions.RemoveEmptyEntries)(1)(0)
It splits the string into substrings using GRTLM as a delimiter, then returns the first character of the second array item.
Or to get the index of N
Dim i = input.Split({s}, StringSplitOptions.RemoveEmptyEntries)(0).Length + s.Length
It splits the string and returns the length of the first array item plus the length of the delimiter string.
But perhaps the simplest way to do it is using String.IndexOf()
Dim n = input(input.IndexOf(s) + s.Length)
Dim i = input.IndexOf(s) + s.Length

Find String in String backwards, starting from specific point

I have a text, in this text I search for a file. I find it by searching for its extension but don't know the name of the file.
When I find this string, I have the index of the extension (indexof(".jpg"))
Now, I would need the full file name. The only thing I know is that the filename starts right after a ">" symbol, but there are many of these in the file. Is there a way for example to get the index of the ">" in a string, starting at a specific index, and going backwards?
Try this regular expression:
>([A-Za-z0-9-+. _]+[.]jpg)
or perhaps better:
>([^[\]\*\/\\<>|]+[.]jpg)
I'm probably missing an allowed character, but that covers most of it and should be easy enough to modify. Use the expression like this:
Public Iterator Function FindJPGFileNames(inpupText As String) As IEnumerable(Of String)
For Each match In RegEx.Matches(inputString, ">([^[\]\*\/\\<>|]+[.]jpg)")
Yield match.Groups(1).Value
Next match
End Function
or
Public Function FindJPGFileNames(inpupText As String) As IEnumerable(Of String)
Dim matches = RegEx.Matches(inputString, ">([^[\]\*\/\\<>|]+[.]jpg)")
Return matches.Select(Function(m) m.Groups(1).Value)
End Function
or in addition I also found this solution:
Private Function searchimages(ByVal s As String, ByVal ending As String) As List(Of String)
Dim los As New List(Of String)
Dim position As Integer = 0
While position <> -1
position = s.IndexOf(ending, position, StringComparison.CurrentCultureIgnoreCase)
If position <> -1 Then
Dim endPosition As Integer = position + ending.Length
Dim startposition As Integer = s.LastIndexOf(">", position - 1, StringComparison.CurrentCultureIgnoreCase) + 1
los.Add(s.Substring(startposition, endPosition - startposition))
position = endPosition
End If
End While
Return (los)
End Function

CountWord and CountVowel into Function in VB.NET

Hi I need to change WordCount and CountVowel procedures to functions and create a function to count number of consonants in a string.
I have done these two procedures but I cannot figure out how to do the last part. I am fairly new to programming.
My current code is given below:
Sub Main()
Dim Sentence As String
Console.WriteLine("Sentence Analysis" + vbNewLine + "")
Console.WriteLine("Enter a sentence, then press 'Enter'" + vbNewLine + "")
Sentence = Console.ReadLine()
Console.WriteLine("")
Call WordCount(Sentence)
Call VowelCount(Sentence)
Console.ReadLine()
End Sub
Sub WordCount(ByVal UserInput As String)
Dim Space As String() = UserInput.Split(" ")
Console.WriteLine("There are {0} words", Space.Length)
End Sub
Sub VowelCount(ByVal UserInput As String)
Dim i As Integer
Dim VowelNumber As Integer
Dim Vowels As String = "aeiou"
For i = 1 To Len(UserInput)
If InStr(Vowels, Mid(UserInput, i, 1)) Then
VowelNumber = VowelNumber + 1
End If
Next
Console.WriteLine("There are {0} vowels", VowelNumber)
End Sub
Thanks for your time
I would use the following three functions. Note that WordCount uses RemoveEmptyEntries avoids counting empty words when there are multiple spaces between words.
The other two functions count upper case vowels as vowels, rather than just lower case. They take advantage of the fact that strings can be treated as arrays of Char, and use the Count method to count how many of those Chars meet certain criteria.
Note that the designation of "AEIOU" as vowels may not be correct in all languages, and even in English "Y" is sometimes considered a vowel. You might also need to consider the possibility of accented letters such as "É".
Function WordCount(UserInput As String) As Integer
Return UserInput.Split({" "c}, StringSplitOptions.RemoveEmptyEntries).Length
End Function
Function VowelCount(UserInput As String) As Integer
Return UserInput.Count(Function(c) "aeiouAEIOU".Contains(c))
End Function
Function ConsonantCount(UserInput As String) As Integer
Return UserInput.Count(Function(c) Char.IsLetter(c) And Not "aeiouAEIOU".Contains(c))
End Function
To turn each of your Sub routines into a Function, you need to do three things. First, you need to change the Sub and End Sub keywords to Function and End Function, respectively. So:
Sub MyMethod(input As String)
' ...
End Sub
Becomes:
Function MyMethod(input As String)
' ...
End Function
Next, since it's a function, it needs to return a value, so your Function declaration needs to specify the type of the return value. So, the above example would become:
Function MyMethod(input As String) As Integer
' ...
End Function
Finally, the code in the function must actually specify what the return value will be. In VB.NET, that is accomplished by using the Return keyword, like this:
Function MyMethod(input As String) As Integer
Dim result As Integer
' ...
Return result
End Function
So, to apply that to your example:
Sub WordCount(ByVal UserInput As String)
Dim Space As String() = UserInput.Split(" ")
Console.WriteLine("There are {0} words", Space.Length)
End Sub
Would become:
Function WordCount(userInput As String) As Integer
Dim Space As String() = UserInput.Split(" ")
Return Space.Length
End Sub
Note, ByVal is the default, so you don't need to specify it, and parameter variables, by standard convention in .NET are supposed to be camelCase rather than PascalCase. Then, when you call the method, you can use the return value of the function like this:
Dim count As Integer = WordCount(Sentence)
Console.WriteLine("There are {0} words", count)
As far as counting consonants goes, that will be very similar to your VowelCount method, except that you would give it the list of consonants to look for instead of vowels.
You could use the Regex class. It's designed to search for substrings using patterns, and it's rather fast at it too.
Sub VowelCount(ByVal UserInput As String)
Console.WriteLine("There are {0} vowels", System.Text.RegularExpressions.Regex.Matches(UserInput, "[aeiou]", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count.ToString())
End Sub
[aeiou] is the pattern used when performing the search. It matches any of the characters you've written inside the brackets.
Example:
http://ideone.com/LEYC30
Read more about Regex:
MSDN - .NET Framework Regular Expressions
MSDN - Regular Expression Language - Quick Reference
VB is no longer a language I use frequently but I don't think I'm going to steer you wrong even without testing this out.
Sub Main()
Dim Sentence As String
Console.WriteLine("Sentence Analysis" + vbNewLine + "")
Console.WriteLine("Enter a sentence, then press 'Enter'" + vbNewLine + "")
Sentence = Console.ReadLine()
Console.WriteLine("")
'usually it's better just let the function calculate a value and do output elsewhere
'so I've commented your original calls so you can see where they used to be
'Call WordCount(Sentence)
Console.WriteLine("There are {0} words", WordCount(Sentence))
'Call VowelCount(Sentence)
Console.WriteLine("There are {0} vowels", VowelCount(Sentence))
Console.ReadLine()
End Sub
Function WordCount(ByVal UserInput As String) As Integer
Dim Space As String() = UserInput.Split(" ")
WordCount = Space.Length
'or just shorten it to one line...
'Return UserInput.Split(" ").Length
End Function
Function VowelCount(ByVal UserInput As String) As Integer
Dim i As Integer
Dim VowelNumber As Integer
Dim Vowels As String = "aeiou"
For i = 1 To Len(UserInput)
If InStr(Vowels, Mid(UserInput, i, 1)) Then
VowelNumber = VowelNumber + 1
End If
Next
VowelCount = VowelNumber
End Function
The most obvious change between a sub and a function is changing the keywords that wrap up the procedure. For this conversation let's just say that's one good word to use for encompassing both concepts since they're very similar and many languages don't really draw such a big distinction.
For Visual Basic's purposes a function needs to return something and that's indicated by the As Integer that I added to the end of both of the function declarations (can't remember if that's the right VB terminology.) Also in VB you return a value to the caller by assigning to the name of the function (also see edit below.) So I replaced those lines that were WriteLines with appropriate assignments. Last I moved those WriteLine statements up into Main. The arguments needed to be changed to use the function return values rather than the variables they originally referenced.
Hopefully I'm not doing your homework for you!
EDIT: Visual Basic underwent a lot of changes to the language during the move to .Net back in the early 2000's. I had forgotten (or possibly not even realized) that the new preferred choice for returning a value is now more in line with languages like C#. So rather than assigning values to WordCount and VowelCount you can just use Return. One difference between the two is that a Return will cause the sub/function to exit at that point even if there is other code afterward. This might be useful inside an if...end if for example. I'm hoping this helps you learn something rather than just being confusing.
EDIT #2: Now that I see the accepted answer and re-read the question it seems there was a small part about counting consonants that got overlooked. At this point I assume this was indeed a classroom exercise and the intended answer was possibly even to derive the consonant count by using the other functions.
Here you go.
Function WordCount(ByVal UserInput As String) As Integer
Dim Space As String() = UserInput.Split(" ")
Return Space.Length
End Function
Function VowelCount(ByVal UserInput As String) As Integer
Dim i As Integer
Dim VowelNumber As Integer
Dim Vowels As String = "aeiou"
For i = 1 To Len(UserInput)
If InStr(Vowels, Mid(UserInput, i, 1)) Then
VowelNumber = VowelNumber + 1
End If
Next
Return VowelNumber
End Function
Function ConsonantCount(ByVal UserInput As String) As Integer
Dim i As Integer
Dim ConsonantNumber As Integer
Dim Consonants As String = "bcdfghjklmnpqrstvwxyz"
For i = 1 To Len(UserInput)
If InStr(Consonants, Mid(UserInput, i, 1)) Then
ConsonantNumber = ConsonantNumber + 1
End If
Next
Return ConsonantNumber
End Function

Function convert cyrillic to latin

I am trying to create custom convert cyrillic to latin text function in VB.net. I've never tried to make a custom function so I don't know what I am doing wrong. I have one problem and also, function doesn't work : Object reference not set to an instance of an object.
Public Function ConvertCtoL(ByVal ctol As String) As String
ctol = Replace(ctol, "Б", "B")
ctol = Replace(ctol, "б", "b")
**End Function** ' doesn't return a value on all code paths
Since I didn't found a solution for cyrillic to latin text I was planning to create function that would replace every letter from one alphabet to another.
You need Return ctol to tell it what value to return.
Perhaps researching "lookup table" would help you make a neater function.
Edit: The Wikipedia entry for Lookup table should be a good start.
Here is a simple example:
Imports System.Text
Module Module1
Function ReverseAlphabet(s As String) As String
Dim inputTable() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
Dim outputTable() As Char = "ZYXWVUTSRQPONMLKJIHGFEDBCA".ToCharArray()
Dim sb As New StringBuilder
For Each c As Char In s
Dim inputIndex = Array.IndexOf(inputTable, c)
If inputIndex >= 0 Then
' we found it - look up the value to convert it to.
Dim outputChar = outputTable(inputIndex)
sb.Append(outputChar)
Else
' we don't know what to do with it, so leave it as is.
sb.Append(c)
End If
Next
Return sb.ToString()
End Function
Sub Main()
Console.WriteLine(ReverseAlphabet("ABC4")) ' outputs "ZYX4"
Console.ReadLine()
End Sub
End Module