Converting long string to individual words in Red/Rebol - string

How can a string with a sentence be converted to a series of words, e.g. convert following string to:
str: "This is a sentence with some words"
to a series of:
["This" "is" "a" "sentence" "with" "some" "words"]
There seems to be a split function in Rebol3 but no such function in Rebol2.
I tried following code with parse but it does not work:
str: "This is a sentence with some words"
strlist: []
parse str [
some got: " " (append strlist got) ]
Error is:
** Script Error: Invalid argument: got
How can this be achieved (a method with parse will be preferable)?

In Rebol 2, this would be:
str: "This is a sentence with some words"
parse str none
resulting in:
["This" "is" "a" "sentence" "with" "some" "words"]
As mentioned in the comments on your post, the documentation. Parse has two modes, one of which is string splitting.
Rebol 3, split will work.

It will be
split str " "
Where split is function. First argument is your string, and second — delimiter.

Related

Delimiters in split function Julia

I got some issues trying to split a sentence in single substrings. Using the split function, I can't manage to use more than a delimiter. The code is:
sentence = "If you're visiting this page, you're likely here because you're searching for a random sentence"
split(sentence, "," , " " , ";")
I get this error:
LoadError: MethodError: no method matching split(::String, ::String, ::String, ::String).
I would like to get an array of single words.
Provide a vector of Chars as the split argument (in Julia quotation " is used for String and apostrophe ' for Char):
julia> split(sentence,[' ',',',';'])
16-element Vector{SubString{String}}:
"If"
"you're"
"visiting"
"this"
"page"
""
"you're"
"likely"
"here"
"because"
"you're"
"searching"
"for"
"a"
"random"
"sentence"

Kotlin, how to use '$' as argument index in String.format

In kotlin how to use the '$' for argument index in the string for formatting.
i.e. would like put the same number 122 at first position and 3rd position of the string. In kotlin the string does not like the '$' for this.
(note: the strFormat are dynamically generated and can't be stored in the string resource file).
val strFormat = "%d, %s, same as 1st int: %1$d, same as 1st string: %1$s, $3$s"
val str = String.format(strFormat, 122, "the string one", "string two")
$ is a special kotlin metacharacter for string templates. You can use the string templating mechanism itself to insert a literal $:
${'$'}

java String.format - how to put a space between two characters

I am searching for a way to use a formatter to put a space between two characters. i thought it would be easy with a string formatter.
here is what i am trying to accomplish:
given: "AB" it will produce "A B"
Here is what i have tried so far:
"AB".format("%#s")
but this keep returning "AB" i want "A B". i thought the number sign could be used for space.
i also tried this:
"26".format("%#d") but its still prints "26"
is there anyway to do this with string.formatter.
It is kind of possible with the string formatter although not directly with a pattern.
jshell> String.format("%1$c %2$c", "AB".chars().boxed().toArray())
$10 ==> "A B"
We need to turn the string into an object array so it can be passed in as varargs and the formatter pattern can extract characters based on index (1$ and 2$) and format them as characters (c).
A much simpler regex solution is the following which scales to any number of characters:
jshell> "ABC^&*123".replaceAll(".", "$0 ").trim()
$3 ==> "A B C ^ & * 1 2 3"
All single characters are replaced with them-self ($0) followed by a space. Then the last extra space is removed with the trim() call.
I could not find way to do this using String#format. But here is a way to accomplish this using regex replacement:
String input = "AB";
String output = input.replaceAll("(?<=[A-Z])(?=[A-Z])", " ");
System.out.println(output);
The regex pattern (?<=[A-Z])(?=[A-Z]) will match every position in between two capital letters, and interpolate a space at that point. The above script prints:
A B

Split String after a specified length but do not break words using groovy

I want to split a string into a maximum character length of 40 without breaking the words.It is fine if the string length is less than 40.
How can i achieve this using groovy script in CPI?
e.g Input = "Here you will find programs to get length of the string"
The 40th position lies at letter "g" of word "length"
Output 1:""Here you will find programs to get"
Output 2: "length of the string"
Here is an approach:
def text = "Here you will find programs to get length of the string"
def result = (text =~ /(?s)(.{1,40})(?:\s|$)/).collect {
it[1]
}
result.each {
println it
}
and here is the output
Here you will find programs to get
length of the string

How to remove the common words from two strings, while keeping the white-space intact using Groovy?

I have two strings,
def str1 = "This is test"
def str2 = "That is test"
I want to find the difference between these two strings using Groovy.
I have tried the -operator but it doesn't seem to work properly.
println 'This is test' - 'That is test'
I want the output to be This That
But, the above code evaluates to the first string This is test. Where am I going wrong? Is there any other way to get the difference between two strings using Groovy?
Minus operator for String works differently - it removes part of String. In your case you get This is test as a result because this String does not contain a substring like That is test.
If you want to get a concatenation of words that are different in both strings you can tokenize both strings and transpose them to a pairs of words and remove pairs that contain the same words. Remaining words can be joined with space character, something like:
def str1 = "This is test"
def str2 = "That is test"
def diff = [str1.tokenize(), str2.tokenize()].transpose() // creates a list of pairs like [["This", "That"], ["is", "is"], ["test", "test"]]
.findAll { it[0] != it[1] } // filters out pairs containing the same word
.flatten() // flats [["This", "That"]] to ["This", "That"]
.join(' ') // creates a final String "This That"
assert diff == 'This That'

Resources