Split string in Go while preserving escaped spaces - go

I can split a string with strings.Split:
strings.Split(`Hello World`, " ")
// ["Hello", "World"] (length 2)
But I'd like to preserve backslash escaped spaces:
escapePreservingSplit(`Hello\ World`, " ")
// ["Hello\ World"] (length 1)
What's the recommended way to accomplish this in Go?

Since go does not support look arounds, this problem is not straightforward to solve.
This gets you close, but leaves a the trailing space intact:
re := regexp.MustCompile(`.*?[^\\]( |$)`)
split := re.FindAllString(`Hello Cruel\ World Pizza`, -1)
fmt.Printf("%#v", split)
Output:
[]string{"Hello ", "Cruel\\ World ", "Pizza"}
You could then trim all the strings in a following step.

Related

How can I do a line break (line continuation) in Kotlin

I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?
For example, adding a bunch of strings:
val text = "This " + "is " + "a " + "long " + "long " + "line"
There is no symbol for line continuation in Kotlin. As its grammar allows spaces between almost all symbols, you can just break the statement:
val text = "This " + "is " + "a " +
"long " + "long " + "line"
However, if the first line of the statement is a valid statement, it won't work:
val text = "This " + "is " + "a "
+ "long " + "long " + "line" // syntax error
To avoid such issues when breaking long statements across multiple lines you can use parentheses:
val text = ("This " + "is " + "a "
+ "long " + "long " + "line") // no syntax error
For more information, see Kotlin Grammar.
Another approach is to go for the 3 double quotes "" pairs i.e. press the double quotes 3 times to have something like this.
val text = """
This is a long
long
long
line
""".trimIndent()
With this approach you don't have to use + , \n or escape anything; just please Enter to put the string in the next line.
trimIndent() to format multi-line strings - Detects a common minimal indent of all the input lines, removes it from every line and also removes the first and the last lines if they are blank (notice difference blank vs empty).

How to remove a long alphanumeric pattern in R

I want to remove a 32 digit ID from a string. The ID is in 32 character/digit format separated by "-"
y <- c("abcd1234-ab12-cd12-1z12-abcd1234abcd6789 Print me I am ok")
What i need is the remaining part of the string "print me I am ok"
appreciate your help!
Will this help?
string stringToTrim = "abcd1234-ab12-cd12-1z12-abcd1234abcd6789 Print me I am ok";
string result = stringToTrim.Substring(IndexOf(" ") + 1);

Swift adding multiple stringByReplacingOccurrencesOfString on one String?

Hello i would like to create a app that changes characters into binary code and i was wondering if there is a way to add multiple stringByReplacingOccurrencesOfString on one String or if i should take another approach to this "Problem".
Here is what i have so far
func textToBinary(theString: String) -> String {
return theString.stringByReplacingOccurrencesOfString("a",
withString: "01100001")
}
textArea.text = textToBinary(lettersCombined)
// lettersCombined is the string that i want to turn into BinaryCode.
Try this:
func textToBinary(theString : String, radix : Int = 2) -> String {
var result = ""
for c in theString.unicodeScalars {
result += String(c.value, radix: radix) + " "
}
return result
}
println(textToBinary("a"))
println(textToBinary("abc", radix: 10))
println(textToBinary("€20", radix: 16))
println(textToBinary("😄"))
(The last one is a smiley face but somehow my browser can't display it).
Edit: if you want to pad your strings to 8-character long, try this:
let str = "00000000" + String(c.value, radix: radix)
result += str.substringFromIndex(advance(str.startIndex, str.characters.count - 8)) + " "
The first line adds eight 0 the left of your string. The second line takes the last 8 characters from the padded string.

How to get the index of a String after the first space in vbsript

I'm trying to grab the first characters before a space.
I know it can be done this way
str = "3 Hello World"
str = Mid(str, 1,2)
But how would i do this after a space?
Edit: Looks like you changed your question to get characters BEFORE the first space instead of AFTER. I've updated my examples.
Here's one way:
strTextBeforeFirstSpace = Split(str, " ")(0)
Assuming a space exists in your string, this would return everything up until the first space.
Another way would be:
strTextBeforeFirstSpace = Left(str, InStr(str, " ") - 1)
You can get the index of the first space with the InStr function
InStr(str, " ")
And use this as a parameter in your Mid function
Dim str, index
str = "3 Hello World"
index = InStr(str," ")
'only neccessary if there is a space
If index > 0 Then
str = Mid(str,1,index - 1)
End If

Processing: replace space with non-breaking space

I keep getting an error of "Badly formed character constant" when using this code in Processing 1.5.1. I want to replace all spaces with non-breaking spaces in my String. Any help would be appreciated.
String newStr;
String S = "Hello there world"
char c = '\u00A0';
newStr = S.replace(' ', c);
Processing indicates unicode without quotes and with the prefix 0xTry this:
String newStr;
String S = "Hello there world"
char c = 0x00A0;
newStr = S.replace(' ', c);

Resources