How to generate string variable with crlf at ttl language - printf

I'm trying to make string that contain crlf inside it at ttl language. For example, somthing such as:
MY_STRING = "hello\nworld"
send MY_STRING
should result as
hello
world
I know I can get the same with:
send "hello" #13#10 "world"
but I need it as string because I want to 'send' this string to another 'function', for example:
MY_STRING = "hello\nworld"
include "printMyString.ttl"
I tried something like:
sprintf2 MY_STRING "%s\n%s" "hello" "world"
sprintf2 MY_STRING "%s%d%d%s" "hello" 13 10 "world"
sprintf2 MY_STRING "%s%d%d%s" "hello" #13 #10 "world"
but it didn't worked, is there is a way?

I found some way, maybe there is better:
code2str CRLF $0d0a
sprintf2 MY_STRING "%s%s%s" "hello" CRLF "world"
send MY_STRING
EDIT
It comes out that my first approach was good, just didn't understand that #13 is string:
sprintf2 MY_STRING "%s%s%s%s" "hello" #13 #10 "world"
send MY_STRING

Related

Remove spaces on new line, but keep the line

I wish to remove spaces from lines that doesn't contain text, but not remove the line. Since a space character can be hard to identify, I will replace the space character with the "#" (hastag character) to showcase the example easier. The string looks something like this:
"This is
########a long string
with many lines
#######
and the above is empty
####this is empty
#############
#######hello"
I wish that the output would remove the spaces on the lines that only contains the space character. I am still using the "#" (hastag character) to showcase the spaces. The final output should look like this:
"This is
########a long string
with many lines
and the above is empty
####this is empty
#######hello"
Without the hashtag character acting as the space character, the expected output should look like this:
"This is
a long string
with many lines
and the above is empty
this is empty
hello"
So to fully clarify, I wish to remove space characters on a line that doesn't contain text, but not remove the line.
Using your example with octothorpes (yet another name for #) and replacing them with spaces in the code, we can use the String.IsNullOrWhiteSpace function to check for such lines and replace them with empty strings:
Module Module1
Sub Main()
Dim s = "This is
########a long string
with many lines
#######
and the above is empty
####this is empty
#############
#######hello"
s = s.Replace("#", " ")
Dim t = String.Join(vbCrLf, s.Split({vbCrLf}, StringSplitOptions.None).
Select(Function(a) If(String.IsNullOrWhiteSpace(a), "", a)))
Console.WriteLine(t)
Console.ReadLine()
End Sub
End Module
Outputs:
This is
a long string
with many lines
and the above is empty
this is empty
hello
Use the following code:
Dim container As String = IO.File.ReadAllText("test.txt")
container = container.Replace(vbNewLine, vbCr).Replace(vbLf, vbCr)
Do While container.Contains(vbCr & vbCr)
container = container.Replace(vbCr & vbCr, vbCr)
Loop
If container.StartsWith(vbCr) Then container = container.TrimStart(Chr(13))
container = container.Replace(vbCr, vbNewLine)
IO.File.WriteAllText("test.txt", container)
It'll trim all the empty new lines and override the file (before vs. after):
Note: If you want to remove the hashes and replace that with white spaces too, just use the following:
Dim container2 As String =
My.Computer.FileSystem.ReadAllText("test.txt").Replace("#", " ")
IO.File.WriteAllText("test.txt", container2)
Try this
Dim myString = "your multiline string here"
Dim finalString As String
For each line In myString.Split(CChar(vbNewLine))
finalstring &= line.Trim() & vbNewLine
Next

How to add spaces for certain condition using 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...

remove from String in VB

I have inserted a option in Dorpdown as follows
<option>إختر </option>
When I select this text from server side on any event I get this value
"إختر       ‎"
Now I want to replace this white space in the string. I have tried replace method of String class. But its not working.
str = str.replace(" ","")
Plz suggest
What you should do first is decode the HTML, such that text like but also & are converted to their textual counterparts (" " and "&"). You can do this with: WebUtility.HtmlDecode. Next you can use String.Trim to remove leading and tailing spaces.
Example:
string s = "إختر ";
string r = WebUtility.HtmlDecode(s).Trim();
Or the VB.NET equivalent:
Dim s As String = "إختر "
Dim r As String = WebUtility.HtmlDecode(s).Trim()
Evidently you can try to convert to spaces yourself. But there are examples where it is not that evident and your transcoder can get confused or decode strings the wrong way. Furthermore if in the future the people at W3C change their minds about how to encode text in HTML/XML, then your program will still work.
String.Trim will remove all kinds of white-space including spaces, new lines, tabs, carriage returns, etc. If you only want to remove spaces, you can use: .Trim(' '). Then you specify only to remove the given list of characters (here only ' ').
If you want to remove leading or trailing white-spaces from a string you just need to use String.Trim, but you have to re-assign the return value to the variable since strings are immutable:
string text = "إختر       ‎";
text = text.Trim();
Note that you can also use TrimEnd in this case.
If you want to remove only space characters(not also tabs or new-line characters which are also white-spaces) use:
text = text.Trim(' ');
If you instead want to remove all spaces from a string you could do:
text = text.Replace(" ", "");
I think maybe your code is something like this
Dim str As String = "إختر "
str.Replace(" ", "")
But actually you should
Dim str As String = "إختر "
str = str.Replace(" ", "")
I have just had a similar problem.
It turns out, that this nbsp character is Chr(160) from the ASCII table. Thus, something like this is quite meaningful, for all the cases. It works, on a selected area:
Public Sub remove_space_in_string()
Dim r_range As Range
For Each r_range In Selection
r_range = Trim(r_range)
r_range = Replace(r_range, vbTab, "")
r_range = Replace(r_range, " ", "")
r_range = Replace(r_range, Chr(160), "")
Next r_range
End Sub

Split string on parentheses and braces

Let me say, I hate working with strings! I'm trying to find a way to split a string on brackets. For example, the string is:
Hello (this is) me!
And, from this string, get an array with Hello and me. I would like to do this with parentheses and braces (not with brackets). Please note that the string is variable, so something like SubString wouldn't work.
Thanks in advance,
FWhite
You can use regular expressions (Regex), below code should exclude text inside all parenthesis and braces, also removes an exclamation mark - feel free to expand CleanUp method to filter out other punctuation symbols:
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim re As New Regex("\(.*\)|{.*}") 'anything inside parenthesis OR braces
Dim input As String = "Hello (this is) me and {that is} him!"
Dim inputParsed As String = re.Replace(input, String.Empty)
Dim reSplit As New Regex("\b") 'split by word boundary
Dim output() As String = CleanUp(reSplit.Split(inputParsed))
'output = {"Hello", "me", "and", "him"}
End Sub
Private Function CleanUp(output As String()) As String()
Dim outputFiltered As New List(Of String)
For Each v As String In output
If String.IsNullOrWhiteSpace(v) Then Continue For 'remove spaces
If v = "!" Then Continue For 'remove punctuation, feel free to expand
outputFiltered.Add(v)
Next
Return outputFiltered.ToArray
End Function
End Module
To explain the regular expression I used (\(.*\)|{.*}):
\( is just a (, parenthesis is a special symbol in Regex, needs to be escaped with a \.
.* means anything, i.e. literally any combination of characters.
| is a logical OR, so the expression will match either left or ride side of it.
{ does not need escaping, so it just goes as is.
Overall, you can read this as Find anything inside parenthesis or braces, then the code says replace the findings with an empty string, i.e. remove all occurrences. One of the interesting concepts here is understanding greedy vs lazy matching. In this particular case greedy (default) works well, but it's good to know other options.
Useful resources for working with Regex:
http://regex101.com/ - Regex test/practice/sandbox.
http://www.regular-expressions.info/ - Theory and examples.
http://www.regular-expressions.info/wordboundaries.html - How word boundaries work.
Try this code:
Dim var As String = "Hello ( me!"
Dim arr() As String = var.Split("(")
MsgBox(arr(0)) 'Display Hello
MsgBox(arr(1)) 'Display me!
Something like this should work for you:
Dim x As String = "Hello (this is) me"
Dim firstString As String = x.Substring(0, x.IndexOf("("))
Dim secondString As String = x.Substring(x.IndexOf(")") + 1)
Dim finalString = firstString & secondString
x = "Hello (this is) me"
firstString = "Hello "
secondString = " me"
finalString = "Hello me"

removing • character in string

i tried to read html contents by striping html tags in a string.
when i try to print that string i got • character. can anyone tell me how to remove this character
Use Replace() function of String
str = "Hello• World"
str = str.Replace("•", "") 'Hello World
Something like:
stringVal = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(stringVal)); //Swaps out non-ascii to '?'s.