How do I make my own word wrap function for strings? I want each line to be no longer than 50 characters and respect existing CRLFs.
Something like this, it will get you started (just a quick samle i mashed together):
Private Sub Doit()
Dim Source As String = ""
Source &= "How to make my own word wrap function for string, I want each line to be no longer than 50chars and take respect existing CRLFs" & vbCrLf & vbCrLf
Source &= "So this will be a new row."
Dim wrappedtext As String = wrap(Source, 20, vbNewLine)
MsgBox(wrappedtext)
End Sub
Function wrap(ByVal text As String, ByVal maxlength As Integer, ByVal newline As String) As String
Dim tmp() As String = Split(text.Replace(vbCrLf, " | "), " ")
Dim ret As String = ""
Dim wrk As String = ""
For Each word As String In tmp
If word = "|" Then
ret &= newline
wrk = ""
ElseIf word = "" Then
Else
If Len(wrk & word) <= maxlength Then
wrk &= " " & word
Else
ret &= wrk & newline
wrk = word & " "
End If
End If
Next
If wrk <> "" Then ret &= wrk
Return ret
End Function
From which point of view? SW architecture?
Take a look at the decorator pattern. If you like to work with streams, in the book "Heads First: Design Patterns" a string modifier is proposed. It's in Java, but the general programming concept is described in a good way. Some pages are missing but you can find many infos here.
The algorithm itself is trivial, isn't it?
Related
I am using the code below to read through each record and get the data. Is there a way I can append the text eachtime to my summarytextbox so it will show all the records/add them one by one?
Dim str As String = Session("List")
For Each s As String In str.Split(","c)
SummaryTextBox.Text = Session("FirstName") & " " & Session("LastName")
Next
I like to use the StringBuilder:
Dim str As String = Session("List")
Dim sb As New Text.StringBuilder()
For Each s In str.Split(","c)
sb.Append(s)
Next
SummaryTextBox.Text = sb.ToString()
... and of course, you can use whatever format you want to display the concatenated results.
EDIT: Like Tim alluded to, I'm not sure what the difference between the List and FirstName and LastName session variables, so I'm just appending the s until further clarification is made by the OP.
Yes.
Dim str As String = Session("List")
For Each s As String In str.Split(","c)
SummaryTextBox.Text &= Session("FirstName") & " " & Session("LastName") & ", "
Next
I'm not sure how that list is related to the firstname and lastname. However, this might give you an idea:
Dim firstName = DirectCast(Session("FirstName"), String)
Dim lastName = DirectCast(Session("LastName"), String)
Dim query = From s In DirectCast(Session("List"), String).Split(","c)
Select String.Format("{0} {1}: {2}", firstName, lastName, s)
SummaryTextBox.Text = String.Join(Environment.NewLine, query)
Stringbuilder is the preferred method. String is immutable, therefor every time a string is appended with & " ", a new string is created. StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed on it.
I want to include multiple messages in one MsgBox using \n. I need to remove this string when the MsgBox is shown.
This is my code.
Dim Msg As String = ""
Dim EmployeeFirstName As String
Dim EmployeeLastName As String
EmployeeFirstName = txtFirstName.Text.Trim
EmployeeLastName = txtLastName.Text.Trim
If EmployeeFirstName = "" Then
Msg = "Please enter First Name!"
Msg += "/n"
End If
If EmployeeLastName = "" Then
Msg += "Please enter Last Name!"
Msg += "/n"
End If
If ddlGender.SelectedItem.Value = -1 Then
Msg += "Plrase Select department"
Msg += "/n"
End If
MsgBox(Msg)
StringBuilder is usually a good choice when you need to build a string dynamically. It often performs better, and generally makes for cleaner, more maintainable code than doing a bunch of string concatenation.
Dim msgBuilder As New StringBuilder()
'...
If EmployeeFirstName = "" Then
msgBuilder.AppendLine("Please enter First Name!")
End If
'And so forth
MsgBox(msgBuilder.ToString())
But, as Matt Wilko points out, if this is ASP.NET, you don't want to use MsgBox at all.
Like this ...
Msg = "Please enter First Name!" & vbCrlf
Msg &= "Please enter Last Name!" & vbCrlf
Msg &= "Please Select department"
I understand I should put the single quotation mark between the string for RowFilter like that
dim TOS as string="04"
rowFilter.RowFilter = "(TOScode= " & "'" & TOS & "')"
I just wonder why the following code still work without single quotation mark.
rowFilter.RowFilter = "(TOScode = " & TOS & ") "
The TOScode field is varchar(2) type in SQL. English is not my native language; please excuse typing errors.
Try this
rowFilter.RowFilter = String.Format("(TOScode = '{0}')", TOS)
OR simply as
rowFilter.RowFilter = String.Format("TOScode = '{0}'", TOS)
It seems I'm not lucky with backslashes in ASP.Net VB.Net.
I'm trying to output some infos about files with ffprobe and my paths are cut randomly at some backslash in every string containing backslash.
I debugged this function to return the path generated by fileToProcess:
Function ffprobe_show_format(ByVal arg As String) As String
Dim myProcess As New System.Diagnostics.Process()
Dim fileToProcess As String = MapPath(".") & "\temps\" & arg
myProcess.StartInfo.FileName = MapPath(".") & "\ffprobe.exe"
myProcess.StartInfo.Arguments = "-show_format " & fileToProcess & " >C:\inetpub\vhosts\mysite.com\httpdocs\absolutefs\ffmpegtemp.txt"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.Start()
Dim myStreamWriter As StreamWriter = myProcess.StandardInput
Dim mystreamreader As StreamReader = myProcess.StandardOutput
Dim str As String = mystreamreader.ReadToEnd
Return str & ";" & "FileToProcess=" & fileToProcess & "MapPath(.) AND ffprobe.exe" & MapPath(".") & "\\ffprobe.exe"
End Function
And it returns ;FileToProcess=C:
It means that
My file is not processed because of some errors
My Path is cut at backslashes
The rest of the string is then broken
Does I need to tell asp.net to represent backslashes in another way?
[EDIT]
I can't choose an answer but will upvote since I made two mistakes:
- To debug, I was reading my value once it was put in SQL variables with limites size then
- ... fetched with Newtonsoft.Json parser that probably was refusing some chars.
I'm new on ASP.Net so I have difficulties finding a good way to debug. So I finally made as usual when I'm not able to debug on a platform: I've written my debug vars in a text file, and everything was there using this way:
Function ffprobe_show_format(ByVal file As String, ByVal app As String) As String
Dim myProcess As New System.Diagnostics.Process()
myProcess.StartInfo.FileName = app
Dim argz As String = FormatCommandLine("-show_format", file)
myProcess.StartInfo.Arguments = argz
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.Start()
Dim mystreamreader As StreamReader = myProcess.StandardOutput str As String = mystreamreader.ReadToEnd
MapPath(".") & "/ffprobe.exe"
Dim objWriter As New System.IO.StreamWriter(MapPath(".") & "\debug.txt")
objWriter.Write(str)
objWriter.Close()
Return str
End Function
I'll add an ffmpeg tag since it's also another example on how to call it and process a file.
You don't need to worry about the backslashes. The backslash is not a special character in VB like it is in C#. In fact if you do double them up you might hit errors.
It's hard to tell where the problem is occurring. Some ideas to try to narrow it down:
Try not redirecting StandardInput since you aren't passing it anything.
Read from StandardError to make sure it is empty. It may contain an error message.
I would make use of Debug.WriteLine to trace the function and try to spot where things begin to go wrong.
This may not matter, but I would place myProcess.WaitForExit after mystreamreader.ReadToEnd.
Larry's suggestion to use System.IO.Path is a good one.
Some hastily modified code based on all this:
Function ffprobe_show_format(ByVal arg As String) As String
Debug.WriteLine("arg: " & arg)
Dim fileToProcess As String = IO.Path.Combine(MapPath("."), "temps\" & arg)
Debug.WriteLine("fileToProcess = " & fileToProcess)
Debug.WriteLine("MapPath AND ffprobe.exe: " & IO.Path.Combine(MapPath(".") & "\\ffprobe.exe"))
Dim myProcess As New System.Diagnostics.Process()
myProcess.StartInfo.FileName = IO.Path.Combine(MapPath("."), "\ffprobe.exe")
myProcess.StartInfo.Arguments = "-show_format " & fileToProcess & " >C:\inetpub\vhosts\mysite.com\httpdocs\absolutefs\ffmpegtemp.txt"
myProcess.StartInfo.UseShellExecute = False
'myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()
'Dim str As String = myProcess.StandardOutput.ReadToEnd
Dim errStr as string = myProcess.StandardError.ReadToEnd
myProcess.WaitForExit()
Debug.WriteLine("myProcess exit code = " & myProcess.ExitCode.ToString())
'Debug.WriteLine("stdOutput: " & str)
Debug.WriteLine("stdError: " & errStr)
Return str & ";" & "FileToProcess=" & fileToProcess
End Function
There are a few things to consider here.
First, the back-slash character is the escape character (i.e. \t \n \r ...) and should be double-escaped \\ IF you MUST embed it into strings.
However, the .NET framework contains a very good class: System.IO.Path that has several Combine method overloads that can help you to construct paths without embedding back-slash characters into strings. This also makes your code more portable in the (somewhat unlikely) event that you would ever run it on a non-Windows platform.
http://msdn.microsoft.com/en-us/library/system.io.path
Try closing the StreamReader before accessing its contents:
Dim myStreamReader As StreamReader = myProcess.StandardOutput
Dim str As String = myStreamReader.ReadToEnd
myStreamReader.Close()
Return str & ";" & "FileToProcess=" & fileToProcess & "MapPath(.) AND ffprobe.exe" & MapPath(".") & "\\ffprobe.exe"
When I run your function in my local devbox:
Function ffprobe_show_format(ByVal arg As String) As String
Dim myProcess As New System.Diagnostics.Process()
Dim fileToProcess As String = HttpContext.Current.Server.MapPath(".") & "\temps\" & arg
myProcess.StartInfo.FileName = HttpContext.Current.Server.MapPath(".") & "\ffprobe.exe"
myProcess.StartInfo.Arguments = "-show_format " & fileToProcess & " >C:\inetpub\vhosts\mysite.com\httpdocs\absolutefs\ffmpegtemp.txt"
'myProcess.StartInfo.UseShellExecute = False
'myProcess.StartInfo.RedirectStandardInput = True
'myProcess.StartInfo.RedirectStandardOutput = True
'myProcess.Start()
'Dim myStreamWriter As StreamWriter = myProcess.StandardInput
'Dim myStreamReader As StreamReader = myProcess.StandardOutput
Dim str As String = String.Empty 'myStreamReader.ReadToEnd
Return str & ";" & "FileToProcess=" & fileToProcess & "MapPath(.) AND ffprobe.exe" & HttpContext.Current.Server.MapPath(".") & "\\ffprobe.exe"
End Function
I get what I consider to be a somewhat normal string returned for ffprobe_show_format:
";FileToProcess=C:\Documents and Settings\Me\My Documents\Proj1\SubApp1\temps\testMapPath(.) AND ffprobe.exeC:\Documents and Settings\Me\My Documents\Proj1\SubApp1\\"
I commented out the System.Diagnostics.Process() as I don't have an executable called ffprobe.exe.
Given that the rest of it works, Try closing the StreamReader before accessing its contents.
You should just need to escape each backslash with another backslash.
Or, you might be able to prefix your string with the # symbol (works in C#, not sure about VB.NET).
I.E.
instead of "C:\this\that.exe"
use "C:\this\that.exe"
or #"C:\this\that.exe"
I have no idea how I'm going to accomplish this BUT someway, somehow I have to perform calculations dynamically in ASP/VB.NET (Possibly SQL Server). Something like this:
Dim var1, var2 as Integer
Dim Optr as string
var1 = 15
var2 = 25
Optr = +
MyResult(var1, var2, Optr)
...and MyResult should equal 40. How can I do this? Anyone have any ideas? Thanks!
The only way I can think of is a switch case which deals with all possible operators.
If you want to implement something more complicated(with operator presedence) you can use the Shunting Yard Algorithm
I don't have the code specifics, but you if have your vars and operations concatenated as a single string you could parse the string and compile it, given you the proper result.
Check this SO questions and answers here, they discuss a very similar problem (if not the exact same)
I like Paul Sasik's example for a purely .net solution, but another alternative would be to make use of the VBScript engine that's part of windows.
For example (you'll need to add a reference to the COM MSScript control)
Static sc As MSScriptControl.ScriptControl
'---- init the script control once
If sc Is Nothing Then
sc = New MSScriptControl.ScriptControl
sc.Language = "VBScript"
sc.AllowUI = False
End If
Try
Return sc.Eval(Expr)
Catch ex As Exception
'Deal with any error conditions here
End Try
Where Expr is any expression you might want to evaluate.
Granted, this leverages VBScript support, which you might want to avoid, but, depending on your audience, they might be more comfortable with that than .net coding
To allow for arbitrarily complex calculations you could take advantage of .NET's on-fly-compilation features. Here's an article for starters.
For your particular implementation you would construct a calculation string from your input like this:
Dim toCalc as string = var1 & " " & Optr & " " & var2
...and use a process where the calculation would get injected into a dynamically created class that would look something like this: (working console sample based on the CP article)
Imports System.CodeDom.Compiler
Imports System.Reflection
Module Module1
Sub Main()
Dim var1 As Integer = 50
Dim var2 As Integer = 65
Dim Optr As String = "+"
Dim equation As String = var1 & " " & Optr & " " & var2
Dim nl As String = System.Environment.NewLine
Dim dynClass As String = ""
dynClass &= "public class DynamicClass" & nl
dynClass &= " public shared function Calc() as double" & nl
dynClass &= " return " & equation & nl
dynClass &= " end function" & nl
dynClass &= "end class" & nl
Try
Dim params As New CompilerParameters
params.ReferencedAssemblies.AddRange(New String() {"Microsoft.VisualBasic.dll"})
Dim mAssembly As Assembly = New VBCodeProvider().CreateCompiler. _
CompileAssemblyFromSource(params, dynClass).CompiledAssembly
Console.WriteLine("Calculating: " & equation)
Console.WriteLine("SUCCESS! Result: " & mAssembly. _
GetType("DynamicClass"). _
InvokeMember("Calc", _
BindingFlags.InvokeMethod Or _
BindingFlags.Public Or _
BindingFlags.Static, _
Nothing, Nothing, Nothing).ToString())
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Module