How to add spaces for certain condition using vb.net? - vb.net

Let say, maximum length in textbox1 is 6 digits. so if the user enter less than 6, i want to add spaces in front of the text. I have no idea how to do that.
Example:
TextBox1 = "123"
output = " 123"

System.String has a method called PadLeft - it adds whatever char you want to the left of the string to make it whatever length you choose:
Dim str As String
Dim pad As Char
str = "123"
pad = "."c ' Using dots instead of spaces so you can see it...
Console.WriteLine(str)
Console.WriteLine(str.PadLeft(6, pad))
Result:
123
...123
You can see a live demo on rextester.
BTW, it also has PadRight...

Related

how to parse string containing Unicode ID's as well as plain text for display in datagrid view [duplicate]

This question already has answers here:
How do I convert Unicode escape sequences to Unicode characters in a .NET string?
(5 answers)
Closed 3 years ago.
I am trying to parse a string (returned by a web server), which contains non-standard (as far as I can tell) unicode Id's such as "\Ud83c" or "\U293c", as well as plain text. I need to display this string, emojis in tact, to the user in a datagrid view.
btw, I am blind so please excuse any formatting errors :(
full example of what my code is parsing: "Castle: \Ud83d\Udc40Jerusal\U00e9m.Miles"
the code I wrote which is failing miserably:
Public Function ParseUnicodeId(LNKText As String) As String
Dim workingarray() As String
Dim CurString As String
Dim finalString As String
finalString = ""
' split at \ char
workingarray = Split(LNKText, chr(92))
For Each CurString In workingarray
If CurString <> "" Then
' remove leading U so number can be converted to hex
CurString = Right(CurString, Len(CurString) - 1)
' attempt to cut off right most chars until number can be converted to text as there is nothign separating end of Unicode chars and start of plain text
Do While IsNumeric(CurString) = False
If CurString = "" Then
Exit Do
End If
CurString = Left(CurString, Len(CurString) - 1)
Loop
If CurString.StartsWith("U", StringComparison.InvariantCultureIgnoreCase) Then
CurString = CurString.Substring(1)
End If
' convert result from above to hex
Dim numeric = Int32.Parse(CurString, NumberStyles.HexNumber)
' convert to bytes
Dim bytes = BitConverter.GetBytes(numeric)
' convert resulting bytes to a real char for display
finalString = finalString & Encoding.Unicode.GetString(bytes)
End If
Next
ParseUnicodeId = finalString
End Function
I tried to do this all kinds of ways; but can't seem to get it right. My code currently returns empty strings, although my guess is that is because of some of the more recent changes I have made to cut off the leading U or to try and chop off one char at a time. If I take those bits out and just pass it something like "Ud83c", it works perfectly; its only when plain text is mixed in that it fails, but I can't seem to come up with a way to separate the two and re-combine at the end.
You can use Regex.Unescape() to convert the unicode escaped char (\uXXXX) to a string.
If you receive \U instead of \u, you also need to perform that substitution, since \U is not recognized as a valid escape sequence.
Dim input as String = "Castle: \Ud83d\Udc40Jerusal\U00e9m.Miles"
Dim result As String = Regex.Unescape(input.Replace("\U", "\u")).
This prints (it may depend on the Font used):
Castle: 👀Jerusalém.Miles
As a note, you might also have used the wrong encoding when you decoded the input stream.

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(""))

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.