How do I find and replace characters in a string in stanza? - stanza

Let's say I have a string val s = "_1.2V_ADC"
The period is invalid for my use case so I need to replace it with another character, like p so the final string should be "_1p2V_ADC"
Is there any easy way to do that in Stanza?

You can use the replace function for this:
val s = replace("_1.2V_ADC", ".", "p")
It will replace all matches of the string "." with the string "p".

You need this function from the core package :
public defn replace (str:String, s1:String, s2:String) -> String
You example become replace("_1.2V_ADC", ".", "p")
That gives _1p2V_ADC

Related

How to replace string in lua?

My question is how can I replace the character from a given string?
If someone says something in the chat, and that word is a bad word then replace it with "****".
I don't know how to approach the print section.. Because I've got the index of the badword but I don't know how to replace it from the original message.
local badwords = {
"badword",
"badword2",
}
function(msg)
local message = msg;
local lowered_msg = message:lower();
for index, val in pairs(badwords) do
if lowered_msg:match(val) then
local indexStart, indexEnd = string.find(lowered_msg, val)
print(message:gsub(indexStart, "*****", indexEnd))
end
end
end
The gsub function takes three arguments:
the string to be modified,
the pattern to be replaced,
and the replacement string,
so you need to do something like this:
message = string.gsub(message, message:sub(indexStart, indexEnd), "*****")
print(message)
This Way use a table with badwords and replacements for gsub.
local badwords = {fair='f**r', foul='f**l'}
print(('fair is foul and foul is fair'):gsub('%w+', badwords))
...if the pattern matches words.
gsub() loops over the whole string except the 4th argument is used

Haskell how to add a character at the start of a string that has passed by recursion

Hi I'm struggling with this and hope someone can help.
I'm trying to add a character at the start of a string that has already been through recursion.
So to explain, the function takes in a string and outputs a string. The recursion code checks for two characters and replaces them with other chars.
So far so good.
At the end before outputting the string I need to add a character at the start of the string.
E.g.the function needs to replace a space with _ and "d" with "g" And put a "/" at the start. So:
Hello world = /Hello_worlg
If you have a function named translate, for example with recursion that transforms "Hello world" to "Hello_worlg", then you can use the list "cons" constructor to put a Char in front, so:
function :: String -> String
function s = '/' : trans s
or shorter:
function :: String -> String
function = ('/':) . trans

How to convert string with backslash to char

I have to convert a backslash string into char, but it seems that casting doesn't exist like in java:
String msg = (Char) new_msg
I have to convert string values like "\t", "\n", "\000"-"\255" to char.
I would first start questioning why you have a single character string in the first place, and whether you can be sure that that is what you actually have. But given that it is, you can get a char from a string, whether it's in the first position or any other, using either String.get, or the more convenient string indexing operator:
let s = "\t"
let c: char = String.get s 0
let c: char = s.[0]
But note that these will both raise an Invalid_argument exception if there is no character at that position, such as if the string is empty, for example.
As an addendum to glennsl's answer, both methods will raise an Invalid_argument exception if the string is empty. If you were writing a function to get the first char of a string, you might use the option type to account for this.
let first_char_of_string s =
try Some s.[0]
with Invalid_argument _ -> None

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 $:
${'$'}

how to separate a string char by char and add a symbol after in c#

Any idea how i can separate a string with character and numbers, for example
12345ABC678 to make it look like this
1|2|3|4|5|A|B|C|6|7|8??
Or if this is not possile, how can i take this string a put every character or nr of it in a different textBox like this?
You can use String.Join and String.ToCharArray:
string input = "12345ABC678";
string result = String.Join("|", input.ToCharArray());
Instead of ToCharArray(creates a new array) you could also cast the string to IEnumerable<char> to force it to use the right overload of String.Join:
string result = String.Join("|", (IEnumerable<char>)input);
use
String aString = "AaBbCcDd";
var chars = aString.ToCharArray();
Then you can loop over the array (chars)

Resources