what is the equivalent of "\n" in vb.net - vb.net

This may seem simple.
It could be vbNewLine
or it can be
https://learn.microsoft.com/en-us/dotnet/api/system.environment.newline?redirectedfrom=MSDN&view=net-6.0#System_Environment_NewLine
However, that is NOT equivalent with "\n"
That is equivalent with
\r\n for non-Unix platforms, or \n for Unix platforms.
What about if I want \n no matter what. \
I tried to search for similar questions and I can't even find it.
There is nothing here either.
https://learn.microsoft.com/en-us/dotnet/api/system.environment.newline?redirectedfrom=MSDN&view=net-6.0#System_Environment_NewLine
So not easy to fine.
Update:
One answer says that "\n" means vbNewLine both in windows and in Linux.
Well, I am writing a vb.net windows program that interact with linux machine. You know, usual API stuff. In which case I need a character in windows that always mean "\n" in linux.
Basically, I need the chr(10) character. Not chr(10)+chr(13) character.
I think the answer I wrote my self is the answer to that.
And I do not think there is a simple answer on that.
Differences Between vbLf, vbCrLf & vbCr Constants may make things clear. However, people that find that question are people that already guess that vbLf may be a solution.
In fact, the questions and the answers over there do not even link "\n" to vbLF at all. They just say that vbLF is line feed. Is it "\n"? Another technicality
This question answer the question more directly. So what's equivalent to linux/unix "\n" no matter what is vbLf

using Microsoft.VisualBasic.CompilerServices;
namespace Microsoft.VisualBasic
{
// Summary:
// Represents a linefeed character for print and display functions.
[__DynamicallyInvokable]
public const string vbLf = "\n";
}
So the answer is
Microsoft.VisualBasic.vbLf
Somehow I can just use vbLf because Microsoft.VisualBasic is so often used it's in my project list I guess.
Update:
One answer says that "\n" means vbNewLine both in windows and in Linux.
Well, I am writing a vb.net windows program that interact with linux machine. You know, usual API stuff. In which case I need a character in windows that always mean "\n" in linux.
Basically, I need the chr(10) character. Not chr(10)+chr(13) character.
I think vbLf is the right answer.
And I do not think there is a simple answer on that.
Differences Between vbLf, vbCrLf & vbCr Constants may make things clear. However, people that find that question are people that already guess that vbLf may be a solution.
This question answer the question more directly. So what's equivalent to linux/unix "\n", which is the line feed chr(10) character no matter what is vbLf

For VB.Net, which tends to run on Windows, the closest map to \n is vbCrLf. This is different than just vbLf, because vbLf always maps directly to the ascii line feed character (10), but \n on some platforms will map to whatever the local system uses for line endings, rather than a simple line feed. On Windows, this is typically the 13/10 vbCrLf pair.
The easiest way to include these in code strings is via the new-ish interpolated strings:
$"This string{vbCrLf}includes some{vbCrLf}line breaks."
If you want to go platform-agnostic, the closest match is Environment.NewLine. And since that's a mouthful to use over and over you can always assing the value to a variable with a shorter name, like this:
Dim vbNl As String = Environment.NewLine

You ought to be using the ControlChars class in VB.NET. ControlChars.Lf is a line feed, i.e. equivalent to "\n", while ControlChars.Cr is a carriage return, i.e. equivalent to "\r". ControlChars.CrLf and ControlChars.NewLine are both equivalent to "\r\n". Environment.NewLine will give you "\r\n", "\n" or "\r", depending on the platform.

Related

Erlang - serialize a string (with no newlines/carriage returns)?

I've seen some answers to this in other programming languages, but not a specific erlang one. I'm able to read the file and I use:
re:replace(Subject, RE, Replacement, Options)
specifically:
re:replace(Content, "\r" , " ",[global,{return,list}]),
to remove those escape sequences (I believe that's what they are called)
And it perfectly replaces all the \r with a space. So my question is:
1) is there a way to put multiple replacements in RE, so I could also remove "\n"?
2) is there a different/better/more efficient way to do it?
It's the basics of regular expressions.
In your first question, you can use the solution:
re:replace(Content, "[\r\n]", " ", [global, {return, list}])
Erlang string syntax is not convenient enough, if you need to do a lot of very complex string processing, you should consider using another language.

How to do a mixed input NSTokenField

I'm trying to get an NSTokenField working that allows editing to a similar post here.
The answer that was provided gave me the key but something is still off. What should the token character set be set as? My tags will be in this format "< token text >". Setting the character tokenizer to " " results in the " " between words being removed.
What should I be using as the token character set? This is driving me crazy!
I haven't tried this, but I would use " " as the tokenizer and then add a space at the end of your display string which is not in your editing string.
So -tokenField:displayStringForRepresentedObject: would return "Hello " and -tokenField:editingStringForRepresentedObject: would return "Hello".
The alternative would be using "<" and ">" as the tokenizing characters, but I could see a lot of potential issues arising from that.

Comma, ')',or valid expression continuation expected

I need my VB.net to write a file containing the following line
objWriter.WriteLine ("TEXTA " (FILEA) " TEXTB")
Unfortunatly the variable (FILEA) is causing problems i now get the error
Comma, ')', or valid expression continuation expected.
Could someone explain this please?
You're not concatenating (joining) the strings proerly...
objWriter.WriteLine ("TEXTA " & FILEA & " TEXTB")
A better style to get into the habit of using is:
objWriter.WriteLine (string.format("TEXTA {0} TEXTB", FILEA))
The FILEA variable replaces the {0} placeholder in the format string. Depending on what the writer you're using is, you may have a formatted overload so you could just do:
objWriter.WriteLine ("TEXTA {0} TEXTB", FILEA)
And since you asked for an explanation;
The compiler is asking you what exactly you want it to do - you've given it 3 variables (String, variable, String) and haven't told it that you want to join them together - It's saying that after the first string "TEXTA", there should either be the closing bracket (to end the method call), a comma (to pass another parameter to the method) OR a "valid continuation expression" - ie something that tells it what to do with the next bit. in this case, you want a continuation expression, specifically an ampersand to signify "concatenate with the next 'thing'".
Presumably you're looking for string concatenation? Try this:
objWriter.WriteLine("TEXTA" & FILEA & "TEXTB");
Note that FILEA isn't exactly a conventional variable name... which leads me to suspect there may be something else you're trying to achieve. Could you give more details?

why does using "\" shows error in jython

I am trying to use a copy command for Windows and we have directories such as c:\oracle.
While trying to execute one such, we get the following error:
source_file=folder+"\"
^
SyntaxError: Lexical error at line 17, column 23. Encountered: "\r" (13), after : ""
Here folder is my path of c:\oracle and while trying to add file to it like:
source=folder+"\"+src_file
I am not able to do so. Any suggestion on how to solve this issue?
I tried with / but my copy windows calling source in os.command is getting "the syntax is incorrect" and the only way to solve it is to use \ but I am getting the above error in doing so.
Please suggest. Thanks for your help
Thanks.
Short answer:
You need:
source_file = folder + "\\" + src_file
Long answer:
The problem with
source_file = folder + "\" + src_file
is that \ is the escape character. What it's doing in this particular case is escaping the " so that it's treated as a character of the string rather than the string terminator, similar to:
source_file = folder + "X + src_file
which would have the same problem.
In other words, you're trying to construct a string consisting of ", some other text and the end of line (\r, the carriage return character). That's where your error is coming from:
Encountered: "\r" (13)
Paxdiablo is absolutely correct about why \ isn't working for you. However, you could also solve your problem by using os.path.normpath instead of trying to construct the proper platform-specific path characters yourself.
In all programming languages I know of, you can't put a quote inside a string like this: "this is a quote: "." The reason for this is that the first quote opens the string, the second then closes it (!), and then the third one opens another string - with the following two problems:
whatever is between the quotes #2 and #3 is probably not valid code;
the quote #3 is probably not being closed.
There are two common mechanisms of solving this: doubling and escaping. Escaping is far more common, and what it means is you put a special character (usually \) in front of characters that you don't want to be interpreted in their usual value. Thus, "no, *this* is a quote: \"." is a proper string, where the quote #2 is not closing the string - and the character \ does not appear.
However, now you have another problem - how do you actually make the escape character appear in a string? Simple: escape it! "This is an escape: \\!" is how you do it: the backslash #1 is the escape character, and the backslash #2 is the escapee: it will not be interpreted with its usual escape semantics, but as a simple backslash character.
Thus, your line should say this:
source=folder+"\\"+src_file
BTW: upvote for both #paxdiablo (who got in before my diatribe) and #Nick (who has a proper Pythonic way to do what you want to do)

Tool to format lines of text into array

I frequently come across this problem. I have a file:
something
something2
something3
which I want output as:
"something","something2","something3"
any quick tool for this, preferably online?
If its just a one off thing, it'd be pretty easy to just do it with a search & replace in any advanced-ish text editor...
For example in notepad++:
Do a Replace (CTRL+H)
Set "Search Mode" to "Extended"
Find: \r\n
Replace with: ","
(of course you'll need an extra quote at the very start & very end of the file).
If you need to do it more than once, writing a small script/program that did a regular expression replace over the file would be fairly straight forward too.
Edit: If you really wanted to do it online, you could use an online regular expression tester (in this case you want to use \n as the regex and "," as your replace pattern, leaving the other settings alone).
A quick Python hack?
lines = open('input.txt').xreadlines()
txt = ','.join(['"%s"' % x for x in lines])
open('output.txt', 'w').write(txt)