How to tell if carriage return is in my string? - vb.net

I have a textbox control that allows for the user of enter button to enter details like ADdresses or other demographic information. Since the default for addresses are as follows:
Address 1
Address 2
City, st
Zip
I am wondering if there is a way to tell if the Enter key was used to make a new line here? I've looked around and currently the only is to have a check in VB for vbCrLf however I'm not seeing it pick this up in the code.
Test data for this would be something similar to below
123 N Street
S Test Street
Test City, XX
91883
The code below is what I'm trying to just replace any return carriage and replace with a space
Text.Replace(vbCrLf, " ")
Will this vbCrLf not pick up a carriage return unless there's an actual space between the above test values?

If you are using a MultiLine textbox (as it seems from your sample) then you don't need to search for the newline characters and replace them with a space.
You could simply use the Lines property where every line is stored separated from the other and then use the string Join method to create a single line string
Dim singleLine = string.Join(" ", myTextBox.Lines)
Of course if you are just interested to know if there is a newline character then just check the Length property of the Lines array

vbCrLf actually refers to two characters, a carriage return (13) and a line feed (10).
I would search and replace each separately. It isn't strictly necessary (as replace will work on the two characters at once), but can catch instances in which the user cut and pasted information, instead of typing directly into the text box.
Text = Text.Replace(vbCr, " ")
Text = Text.Replace(vbLf, " ")
or
Text = Text.Replace(vbCr, " ").Replace(vbLf, " ")
https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.constants.vbcrlf(v=vs.110).aspx

The Replace() function will indeed properly detect and replace all occurrences of the target with the replacement - it does not matter whether there are leading or trailing spaces.
However, please consider that String objects are immutable and cannot be changed after they have been instantiated. Therefore, Replace() does not modify the existing object but rather returns a new string as its result.
To actually see the results of the function call, you need to do something along these lines:
newString = Text.Replace(vbCrLf, " ")

I spent quite some time to resolve exactly this problem, the solution I came across was:
text = text.Replace(" ", ControlChars.CrLf)
Sorry cant remember where I found the solution but if I do remember it I will post the link here.

Related

Multi line string separation in velocity template

Using the velocity template language and I have a multiline string. I am trying to find the end of line so I can split these strings. But somehow I am not able to find it. Used all the functions like replace, replaceAll, indexof, split for \n, \n, but it does not recognize the end of line in the multiline character. I have this template in the work document.
#set($st="abc
def
hij
")
#set($index = $st.indexOf(ā€œ\nā€))
Always returns -1. Tried different things to find the carriage return in velocity template. But does not work. One of the string I have is:
#set($st="abc
")
and I want to change it to:
"abc"
instead of
"abc
"
So I need to find the end of line. Any help is appriciated.

Unwanted Linebreak in VBA-Variable

I'm pretty much a newbie to VBA and Access and I start to get kinda frustrated.
I'm working on a program for calculating prizes and therefor I got HTML-Datas with the necessary informations which get split up as following:
strHTML = GetHTML(HTML)
List() = Split(strHTML, Chr(10))
Just to make it clear, the HTML lines I pick up for this look as following:
Motortyp=SM132
Motortypreihe=SM
MotorBG=132
MotorFE=35
MotorZusatz1=B
MotorZusatz2=
ReglerTyp=Fremdregler
So far so good.
The whole gear description (e.g. "SM200.15C") is taken at once and somehow there still is kind of a line break afterwards which leads to me not being able to use it in queries as I want to.
It seems like the line break at which I split up the HTML somehow is still present and it just don't want to get replaced by stuff like Replace([Data], Chr(10). "").
Hope the question was Kind of understandable since I got a few brain lags today.
Thanks in advance,
Dennis
On Windows a newline is obtained from the combination of a carriage return Chr(13) and linefeed Chr(10) characters. Use
List() = Split(strHTML, vbCrLf)
where vbCrLf is a VB constant representing this pair of characters.

In Excel VBA, how do I refer to a named column that contains a single quote

I am investigating a bug in an Excel spreadsheet where the following formula is inserted into every cell in a column.
=AGGREGATE( 3, 5, InputData[#[Foo]:[Bar]]) > 0
The VBA is as follows:
Let AddColumn.DataBodyRange.formula = "=AGGREGATE( 3, 5, [#[Foo]:[Bar]]) > 0"
this will evaluate to FALSE if all of the cells on the current row between columns Foo and Bar are empty, otherwise it evaluates to TRUE
The problem I'm seeing is that the names Foo and Bar are variable and not under my control and the formula fails with Run-time error 1004 if a name contains a single quote:
Let AddColumn.DataBodyRange.formula = "=AGGREGATE( 3, 5, [#[Foo's name]:[Bar]]) > 0"
Is there a way I can escape the name in such a way that single quotes won't create the run-time error? Adding double quotes around the name gives me the same error.
Are there likely to be further problems if the names contain other characters that have special meaning in Excel?
I could also refer to the columns by address instead of name. Would that work with the current row '#' notation?
Excel version:14.0.7188.5002
I hear you when you say the naming convention is "not under my control". This really puts you in a bind when anything can be pumped into your code.
Sadly, the only solution is to scrub the input when they finally hand it over to you. This involves you having to make your own vba function that takes in a string and returns a string that has special characters removed (or replaced with something else).
In your case, you are going to have to scrub the data in possibly two places.
First, you will need to change all the column names so they don't have special characters in them. You'll need to access each name and send it through the 'scrub' function and then replace the name with the scrubbed name.
Second, when someone inputs a column name for your AGGREGATE, you'll need to capture that input into a string variable and then pass that through the 'scrub' function. Then you'll need to validate that the input they gave you matches up with a valid column name. If it's not valid, send them an error message asking them to enter a valid name or to cancel out.
After you have valid values for foo and bar, you can add them to your AGGREGATE function and let it execute.
If you can't scrub/change your column names, then you'll have to make a list of scrubbed column names and associate them with the column address. Then when you get your input, scrub it, and then match to the list to grab the correct address. Then you can use hard addresses instead of variable naming schemes.
It's a lot of work. But it's necessary when you have naming conventions that are not under your control.
The other answers and comments put me on the right track:
Function escapedColumnName(columnName As String) As String
columnName = Replace(columnName, "'", "''")
columnName = Replace(columnName, "#", "'#")
columnName = Replace(columnName, "[", "'[")
columnName = Replace(columnName, "]", "']")
escapedColumnName = columnName
End Function

Visual Basic pulling a character from a certain line number

I am working on a macro that needs to be able to check and make sure that a character on a certain line is what it should be before it finishes the form.
Where or how do i find the information to do this?
I have tried using the command "Left(#of line ,1)" and am not getting it to return anything at this point.
I Assume that you have this text in a string. Comment to my answer if wrong.
You can use a Regex to compare to string with your character like so :
If Regex.IsMatch(MyString, *.{X}Y) Then
'You have to manually replace X for the number of characters before the character you scan for
'You have to manually replace Y for the-said character it is supposed to be
Look at http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial for more info on Regex.
If you want to only take one line in your string :
TheLine = split(MyString,Environnement.Newline)([Line Number])

Read a text file and display result in a window

I have a text file which contains about 60 lines. I would like to parse out all the text from that file and display in a window. The text file contains words that are separated by an underscore. I would like to use regular expression to solve this problem.
Update:
This is my code as of now. I am trying to read "filename" in my code.
Dim filename = "D:\databases.txt"
Dim regexpression As String = "/^[^_]*_([^_]*)\w/"
I know I don't have much done here anyway but I am trying to learn VB on my own and have gotten stuck here.
Please feel free to suggest what I should be doing instead.
Something like this:
TextBox1.Lines = IO.File.ReadAllLines("fileName")
To remove underscores:
TextBox1.Lines = IO.File.ReadAllLines("fileName").Replace("_", String.Empty)
If you also need other special characters removed, you can use Regex.Replace:
Remove special characters from a string
Also on MSDN:
How to: Strip Invalid Characters from a String
Or the old school way - loop through all characters, and filter only those you need:
Most efficient way to remove special characters from string