String range alternative in vb.net - vb.net

Is there any function similar to string range in vb.net 2.0 ??
What I am try to achieve here is to extract some text from a string with unknown length.
eg.
given string = text text text mytext1 text text text text mytext2 text text text text
expected string = mytext1 text text text text mytext2
So I have the indexes for "mytext1" and "mytext2". I am looking for a way to get the text that wrapped in between those two strings or indexes.
Thanks

Well, what’s wrong with String.Substring? It works on indices so if you want to find text delimited by two words, you first need to find their respective indices using String.IndexOf.
Dim from = given.IndexOf("mytext1")
Dim [to] = given.IndexOf("mytext2")
Dim result = given.Substring(from, [to] - from + "mytext2".Length)
(Note that To is a reserved word so I need to put the identifier in square braces … or use another identifier. ;-))

Assuming that your End Index is at the end of "myText2" then you can do this...
Dim strExpectedString = Mid(strGivenString, intStartIndex, (intEndIndex - intStartIndex))
Otherwise just add the length of "myText2" to intEndIndex.

Related

Override String in TextBox

I have 3 text boxes on my form: Surname, First Name and Middle Name. I want to override my string. It looks like this:
...<<<<<<<<<<<<<<<<... (Length = 36)
If I save a value and the text boxes contain Surname:Bergs, First Name:John Paul, Middle Name:Dale, and for sample purpose I want to display this in MessageBox, and should be like this
D<aleBergs<<John<Paul<<<<<<<<
I have 2 objectives in this Question.
How can I get the Text box specific value and display it on my string?
If Text Box Contains space how can I turn it into < value?
Update
I solved Second Problem fix using this String.Replace()
Dim str As String = "John Paul"
Dim str2 As String = str.Replace(" ", "<")
MessageBox.Show(str2)
Use pre-fabricated string to have clear format
Dim outputFormat As String = "{0} {1} {2}"
MessageBox.Show(string.Format(outputFormat, txtMid.Text, txtFirst.Text, txtLast.Text))
You can also use format like {0, 10} or {0,-10} to get fixed positions, like this
|John......|
|......John|
Where dots stand for spaces
If i'm right, You can get textbox inputs to separate string variables and concatenate them into one string variable or you can get all textbox values to one string variables like
Dim AllStrings As String
AllStrings = MiddleTextBox.Text &" "& FirstTextBox.Text &" "& LastTextBox.Text
Then you can replace spaces by using Replace Method.
AllStings = AllStrings.Replace(" ","<")
Hope this will helpful to you.

VB2010 String adds up Length by adding ""

I'am comparing Strings in Visual Basic 2010 Express. While cuting the String together it sometimes adds a Char with "", what I hoped is "nothing"
Example:
Dim text as String = "test"
Dim sign as Char = ""
text = text + sign
while debuging it says that the new text is "test", but if I ask for the Length it is 5.
This is a problem when I try to compare this with an other String
Dim bigtext as String = "test1234"
Dim text as String = "test"
Dim sign as Char = ""
text = text + sign
bigtext.indexOf(text) 'should be 0 (index), but is -1 (not found)
any idea how to filter a "" away or any other workaround?
Edit - my workoround for now:
Now I add "§" everywhere instead of "" and when I need to use indexOf() to compare something, I Replace("§", "") it.
(with Replace() it is deleted)
As far as I can see, a Char variable always has a character in it (which can be the null character). Concatenating it to another string will append that character to the existing string.
I see two workarounds:
Use a String for sign instead of a Char. The string could be empty or have a single character in it.
Trim the undesired character from the resulting string:
text = (text + sign).Trim(CChar(""))

Displaying each element of array on new line

Please consider I am very new with VB.NET when attempting to read and answer my question
I have a textbox that takes in a list of words separated with a comma on the same line. When button is clicked string gets assigned to variable text and I then split it to variable arrayText.
Then I loop over it and display each element of array on new line.
My code looks as follows
Dim text As String
Dim arrayText() As String
text = TextBox1.Text
arrayText = text.Split(",") 'every "," generates new array index, removes ","
text = ""
For i = 0 To arrayText.Length Step 1
text = arrayText(i) & vbCrLf
MsgBox(text)
Next
When debugging I get error message array out of bounds, however when I remove the newline character (vbCrLf) it displays my text, word for word in a messagebox (which I am using for debugging) and at the end of the loop it kicks out with same error message.
What am I doing wrong here, any improvement suggestions?
Though walther answer is right, i suggest you to use a List(Of String) and a For Each...Next loop.
A list is more "modern" and most of the times it is preferred over an array in vb.net. You can use Environment.NewLine instead of vbCrLf. I'm not sure what exactly you want to do, but I dont think that using a MsgBox is the optimal way to present the seperated words. Here is a simple example of what I think you should do :
' Hold the text from the text box.
Dim FullText As String = TextBox1.Text
Dim SeperatedWords As New List(Of String)
' ToList function converts the array to a list.
SeperatedWords = FullText.Split(",").ToList
' Reset the text for re-presentation.
FullText = ""
' Goes through all the seperated words and assign them to FullText with a new line.
For Each Word As String In SeperatedWords
FullText = FullText & Word & Environment.NewLine
Next
' Present the new list in the text box.
TextBox1.Text = FullText
For i = 0 To arrayText.Length - 1 Step 1
Last element has index of length of array - 1.

How to get the first value in textbox

how can i get the first value in a text box and place it to a label. Example, I want to get the word "look" in the textbox that I input was "look like".
You could use substring, this will split the string or aka your textbox.
This takes two params, The starting index of the string and the 2nd param is the length. I have put str.indexof in the 2nd param to get the index of where the space is.
dim str as string
Label1.Text = str.Substring(0, str.IndexOf(" "))
This code is not tested, I only used visual basic for a little bit when i was 12.
you can simply split the value by " " space and take the first one from result array
if(!String.IsNullOrEmpty(textBox1.Text))
{
lable1.Text = textBox1.Text.Split(" ").first();
}
textboxValue = Textbox.Text;
str = textboxValue.Split(" ").first();

VB.NET Get text in between Quotations or other symbols

I want to be able to extract a string in between quotation marks or parenthesis etc. to a variable. For example my text might be "Hello there "Bob" ". I want to extract the text "Bob" from in between the two quotation marks and put it in the string "name" for later use. The same would be for "Hello there (Bob)". How would I go about this? Thanks.
=======EDIT======
Sorry, I worded this poorly. Ok, so lets say I have a textbox(Textbox1) and a button. If the user inputs the text: MsgBox "THIS IS MY MESSAGE" I want that when the Button is pressed, only the text THIS IS MY MESSAGE is displayed.
This is a solution very simple:
Dim sAux() As String = TextBox1.Text.Split(""""c)
Dim sResult As String = ""
If sAux.Length = 3 Then
sResult = sAux(1)
Else
' Error or something (number of quotes <> 2)
End If
There are basically three methods -- regular expressions, string.indexof and substring and finally looping over the characters one by one. I would avoid the latter as it is just reinventing the wheel. Whether to use regexs or indexof depends upon the complexity of your requirements and data. Indexof is a bit wordy but fairly straightforward and possibly just what you want in this case.
Dim str as String = "Hello there ""Bob"""
Dim startName as Integer
Dim endName as Integer
Dim name as String = ""
startName = str.IndexOf("""")
endName = str.Indexof("""", If(startName > 0, startName,0))
If (endName>startName) Then
name = str.SubString(startName, endName)
End If
If you need to do this for arbitrary symbols, then you want regexs.