Deleting string of a string to its end in vb.net - vb.net

if i have this:
Dim str as String = "This is a string to delete"
i used the contain method to detect the "String" inside the str if found the "String" will be deleted also the next character to the last character of the str.
str = "This is a "

One way to remove the last part of the string is to use IndexOf to find the location of the part you want to delete, and Substring to delete it
Dim str As String = "This is a string to delete"
Dim loc As Integer = str.IndexOf("string")
If loc >= 0 Then str = str.Substring(0, loc)

It sounds like you are wanting to remove a specified string, plus the first character that follows that string. If that is correct, it can be done like this:
Dim str As String = "This is a string to delete"
Dim stringToRemove As String = "string"
str = str.Replace(str.Substring(InStr(str, stringToRemove) - 1, stringToRemove.Length + 1), "")

Related

Remove a word in a String

I have problem in removing word in my array because it is a generated GUID.
And now I need is to remove 1 of word or item in that string.
Here is my String:
Dim guid_id as string ='3a0eed1f-73b2-11e0-8670-88006707ed92','3a125s34-73b2-11e0-8670-88006707ed92','3a112w3s-73b2-11e0-8670-88006707ed92'
Q: How can i remove the word or string " '3a112w3s-73b2-11e0-8670-88006707ed92' " in that 1 whole string?
I have idea that I need to convert it into List(of String) but I don't know how to remove it in that list.
You can simply find and replace the string using Replace function
Dim guid_id as string = "'3a0eed1f-73b2-11e0-8670-88006707ed92','3a125s34-73b2-11e0-8670-88006707ed92','3a112w3s-73b2-11e0-8670-88006707ed92'"
Dim strRemove As String = "'3a112w3s-73b2-11e0-8670-88006707ed92'"
guid_id = guid_id.Replace(strRemove, "").Trim()
If guid_id.Subtring(0,1) = "," Then guid_id = guid_id.Substring(1);
If guid_id.Subtring(guid_id.Length-1) = "," Then guid_id = guid_id.Substring(0, guid_id.Length-1);
.NET Framework has an engine for text processing, which is represented by the System.Text.RegularExpressions.Regex. You can use it to replace a specific word in a string.
Try this code:
Dim guid_id As String = "'3a0eed1f-73b2-11e0-8670-88006707ed92','3a125s34-73b2-11e0-8670-88006707ed92','3a112w3s-73b2-11e0-8670-88006707ed92'"
Dim strRemove As String = "'3a112w3s-73b2-11e0-8670-88006707ed92'"
'To remove strRemove from string, we use Regex replace method
Dim regex = New Regex(strRemove, RegexOptions.IgnoreCase)
guid_id = regex.Replace(guid_id, "")
'Now we remove 'Comma' from string, if it is needed.
If guid_id.Subtring(0, 1) = "," Then guid_id = guid_id.Substring(1)
If guid_id.Subtring(guid_id.Length - 1) = "," Then guid_id = guid_id.Substring(0, guid_id.Length - 1)

VB.net Function returning inconsistent results

I created a simple function designed to remove a string of characters from another string and replace it with what ever string the user wants (or no string as a default)
Private Function RemoveString(scontainer As String, Optional rcontainer As String = "", Optional rstring As String = "") As String
Dim container As String = scontainer
Dim tcontainer As String
If InStr(container, rcontainer) <> 0 Then
Do While (InStr(container, rcontainer) <> 0)
tcontainer = Microsoft.VisualBasic.Left(container, InStr(container, rcontainer) - 1)
tcontainer = tcontainer & rstring & Microsoft.VisualBasic.Right(container, (Len(container) - (InStr(container, rcontainer) + 2)))
container = tcontainer
Loop
RemoveString = container 'return modded string
Else
RemoveString = scontainer 'return string as is
End If
End Function
The problem is:
While this is suppose to be a general use function, I really need it to be concerned with 2 different strings
%20
amp;
the function works perfectly for the %20 situation but it leaves the semi-colon behind for the amp; string. Any ideas why this might be?
Do I get you right ?
You want to replace a certain char sequence in your string with another char sequence or just delete it.
If thats the case you could use String.Replace(oldValue As String, newValue As String) As String
Dim startString as String = "%20 amp;"
Dim resultString as String = startString.Replace("%20 ",String.Empty)
resultString = resultString.Replace(";",String.Empty)
After these lines resultString would be "amp"

Flip letters of a string - Visual Basic

Simple coding assignment: Take text from a textbox and flip it so it's backwords:
i.e. Hello My Name Is David would be "divad si eman ym olleh" ( The program doesn't have to match case, just the letters)
This is something I found, do you have any other methods?
Dim str As String = Textbox1.Text
Dim arr As New List(Of Char)
arr.AddRange(str.ToCharArray)
arr.Reverse()
For Each l As Char In arr
lblOne.Text &= l
Next
You can do it in one line with using the StrReverse function (in Microsoft.VisualBasic).
Dim myText As String = "My Name is Dave"
Dim revText As String = StrReverse(myText)
Quick one liner.
lblOne.Text = String.Join("", "divad si eman ym olleh".Reverse())
Microsoft.VisualBasic
Dim myText As String = My Name is abc
Dim revText As String = StrReverse(myText)
Output: "cba si eman ym"
You can use String.Join instead of looping through each character and concatenating:
lblOne.Text = String.Join("", arr)
create a function that accepts string an returns a reversed string.
Function Reverse(ByVal value As String) As String
Dim arr() As Char = value.ToCharArray()
Array.Reverse(arr)
Return New String(arr)
End Function
and try using it like this,
lblOne.Text = Reverse(Textbox1.Text)
Here is a similar way but with fewer number of lines.
Dim Original_Text As String = "Hello My Name is Ahmad"
Dim Reversed_Text As String = ""
For i = Original_Text.Length To 1 Step -1
Reversed_Text &= Original_Text.Substring(i, 1)
Next
The simplest method to reverse a string is :
Dim s As String = "1234ab cdefgh"
MessageBox.Show(s.AsEnumerable.Reverse.ToArray)
First Create a textbox, it will be TextBox1
then create a button and name it Reverse
then Create a label, it will be Label1
Now double click on Reverse Button (Go to Button Click Event)
and type following code.
and run software And type your string in textbox and click on reverse button.
Dim MainText As String = TextBox1.Text
Dim revText As String = StrReverse(MainText)
Label1.Text = revText

How to remove a character from at string at certain position from the end?

I have a string, for example:
Dim str as string = xxxxxxxxxxxxxxxxxxxx£xxx£xxxx**£**xxxxxxxxxx
I want to remove £ surrounded from * which is always at a certain position (11th for instance) from the end. The whole string is a long one, always change in size and cannot be counted from the start. I cannot use Replace as well, there may be same characters at other positions that I do not wish to remove.
Solution:
Dim rst As String = str.Remove(str.Length - 11, 1)
Edit: Whoops, I dunno what I was thinking on that first part.
The correct version of the first part would be:
str = str.Substring(0, str.Len -13) + str.Substring(str.Len-11);
There also may be an overload for the String.Delete function that allows you to use a negative number to represent the number of characters from the end of the string -- I know that the C# equivalent does.
If its always going to be the 11th character from the end you can do this...
Dim strTargetString As String = "xxxYxxxxxxxxxx"
Dim strTargetString2 As String = "xxxxxxxYxxxxxxxxxx"
Dim strResult As String = Mid(strTargetString, 1, (Len(strTargetString) - 11)) & Microsoft.VisualBasic.Right(strTargetString, 10)
Dim strResult2 As String = Mid(strTargetString2, 1, (Len(strTargetString2) - 11)) & Microsoft.VisualBasic.Right(strTargetString, 10)
Note that String.SubString is a more modern approach than Mid, but I use it out of preference and example.
This is fairly straightforward with a regular expression replacement operation using look-ahead:
Dim str as String = "xxxxxxxxxxxxxxxxxxxx£xxx£xxxx£xxxxxxxxxx"
Dim str2 as String = Regex.Replace(str, "£(?=.{10}$)", String.Empty)
This will target a single character followed by any ten characters then the end of the string and replace it with the String.Empty value (or just "" if you'd prefer).

Remove special characters from a string

These are valid characters:
a-z
A-Z
0-9
-
/
How do I remove all other characters from my string?
Dim cleanString As String = Regex.Replace(yourString, "[^A-Za-z0-9\-/]", "")
Use either regex or Char class functions like IsControl(), IsDigit() etc. Get a list of these functions here: http://msdn.microsoft.com/en-us/library/system.char_members.aspx
Here's a sample regex example:
(Import this before using RegEx)
Imports System.Text.RegularExpressions
In your function, write this
Regex.Replace(strIn, "[^\w\\-]", "")
This statement will replace any character that is not a word, \ or -. For e.g. aa-b#c will become aa-bc.
Dim txt As String
txt = Regex.Replace(txt, "[^a-zA-Z 0-9-/-]", "")
Function RemoveCharacter(ByVal stringToCleanUp)
Dim characterToRemove As String = ""
characterToRemove = Chr(34) + "#$%&'()*+,-./\~"
Dim firstThree As Char() = characterToRemove.Take(16).ToArray()
For index = 1 To firstThree.Length - 1
stringToCleanUp = stringToCleanUp.ToString.Replace(firstThree(index), "")
Next
Return stringToCleanUp
End Function
I've used the first solution from LukeH, but then realized that this code replaces the dot for extension, therefore I've just upgraded the code slightly:
Dim fileNameNoExtension As String = Path.GetFileNameWithoutExtension(fileNameWithExtension)
Dim cleanFileName As String = Regex.Replace(fileNameNoExtension, "[^A-Za-z0-9\-/]", "") & Path.GetExtension(fileNameWithExtension)
cleanFileName will the file name with no special characters with extension.