It is necessary to replace all characters of the string with the adjacent character (shift all characters to the right by 1) - kotlin

It is necessary to replace each character in the string cyclically with the character adjacent to the right, and then collect it into a string again.
Instead of shifting characters to the right, it turns out to increase alphabetically
fun main {
val message = "abcd1234"
val messageSecond = message.map {char -> char + 1}.joinToString ("")
}

Your code is wrong as it increases the ASCII code of each character and as you noted yourself it increases "alphabetically" (not exactly, just in ASCII order)
I can't write the code for you as it seems to be an assignment, but I can give you a hint.
You can actually solve this just by "moving" only one character.
 
Best of luck!

Related

Escape hex like \u... in kotlin strings

I have a string "\ufffd\ufffd hello\n"
i have a code like this
fun main() {
val bs = "\ufffd\ufffd hello\n"
println(bs) // �� hello
}
and i want to see "\ufffd\ufffd hello", how can i escape \u for every hex values
UPD:
val s = """\uffcd"""
val req = """(?<!\\\\)(\\\\\\\\)*(\\u)([A-Fa-f\\d]{4})""".toRegex()
return s.replace(unicodeRegex, """$1\\\\u$3""")
(I'm interpreting the question as asking how to clearly display a string that contains non-printable characters.  The Kotlin compiler converts sequences of a \u followed by 4 hex digits in string literals into single characters, so the question is effectively asking how to convert them back again.)
Unfortunately, there's no built-in way of doing this.  It's fairly easy to write one, but it's a bit subjective, as there's no single definition of what's ‘printable‘…
Here's an extension function that probably does roughly what you want:
fun String.printable() = map {
when (Character.getType(it).toByte()) {
Character.CONTROL, Character.FORMAT, Character.PRIVATE_USE,
Character.SURROGATE, Character.UNASSIGNED, Character.OTHER_SYMBOL
-> "\\u%04x".format(it.toInt())
else -> it.toString()
}
}.joinToString("")
println("\ufffd\ufffd hello\n".printable()) // prints ‘\ufffd\ufffd hello\u000a’
The sample string in the question is a bad example, because \uFFFD is the replacement character — a black diamond with a question mark, usually shown in place of any non-displayable characters.  So the replacement character itself is displayable!
The code above treats it as non-displayable by excluding the Character.OTHER_SYMBOL type — but that will also exclude many other symbols.  So you'll probably want to remove it, leaving just the other 5 types.  (I got those from this answer.)
Because the trailing newline is non-displayable, that gets converted to a hex code too.  You could extend the code to handle the escape codes \t, \b, \n, \r and maybe \\ too if needed.  (You could also make it more efficient… this was done for brevity!)
Simply escape the \ in your strings by adding another backslash in front of it:
val bs = "\\ufffd\\ufffd hello\n"
You can also use raw strings with """ so you don't have to escape the backslashes (which is useful for regex):
val bs = """\ufffd\ufffd hello\n"""
Note that in that case the \n would also NOT be counted as an LF character, and will be literally printed as the 2 characters "\n".
You can add literal line breaks in your raw string if you want an actual line feed, though:
val bs = """\ufffd\ufffd hello
"""

efficiently break Kotlin string into fixed length sub-strings without using regex

obviously to split can be used to break a string in to sub-strings at specific character or delimiter string, but i was looking for any easy way to break into fixed length sub-strings.
eg.
"abcde".splitAt(2) == listOf("ab", "cd", "e")
any ideas?
Use the CharSequence.chunked(size: Int) function. It does exactly that:
println("abcde".chunked(2)) // [ab, cd, e]

Do swift chars relate to clang chars?

Swift's String.Index is defined in the docs as
A position of a character or code unit in a string.
The endIndex is
A string’s “past the end” position—that is, the position one greater
than the last valid subscript argument.
and startIndex is
The position of the first character in a nonempty string.
Is it correct to think of Swift chars in the context of C lang chars?
My understanding in Clang is that the index of a character in a string returns the memory space of the character and the string ends on a null character.
So for Swift, the endIndex is the null character and the reason we use String.Index instead of using subscript String with an Int (something like string[0] like in say JavaScript) is because we are handling the memory space of the character.
If this is the right thinking is this because Swift runs on top of Objective-C runtime?

how to cut part of string

How to cut part from this string...
"abb.c.d+de.ee+f.xxx+qaa.+.,,s,"
... where i know position by this:
Result is always between "." (left side of result) and "+" (right side).
I know number of "." from left side and number of "+" from right side, to delimit resulting string.
Problem is right side, cause i need to count "+" from end.
Say...
from left side: begining is at 4th "."
( this is easy ), result is =
"xxx+qaa.+.,,s,"
from right side: end is at second "+" from end!
"xxx[here]+qaa.+.,,s,"
result is =
"xxx"
I try to do this myself with .substring and .indexOf, but with no success...
Any ideas? thanks
You could use the StrReverse function to reverse the character sequence and then count + from the left (using the same method as counting the .).
To find the start of the substring, loop through the string from the left. Count the number of .s you have seen and stop when you've hit the number you want. Store the index in some variable like start.
Similarly to find the end of the substring, loop from the right and count +s.
You can solve this problem using Regex:
Dim r As New Regex("^(.*\.){4}(?<value>.*)(\+.*){2}$")
Dim m As Match = r.Match("abb.c.d+de.ee+f.xxx+qaa.+.,,s,")
Dim result As String = m.Result("${value}")
Explanation
^ Indicates the beginning of the string
(.*\.){4} This means any character (.) repeated any number of times (*) followed by a period (\.). The period has to be escaped with the backslash because otherwise the period would be the any-character wildcard. The .*\. is enclosed in (){4} to say that pattern must repeat four times.
(?<value>.*) This specifies the placeholder for the text we are after. value is the name we are assigning to it. The .* specifies that the value is any number of any characters.
(\+.*){2} This means a plus character (has to be escaped) followed by any number of any characters, repeated twice.
$ Indicates the end of the string

How can I write special character in VB code

I have a Sql statament using special character (ex: ('), (/), (&)) and I don't know how to write them in my VB.NET code. Please help me. Thanks.
Find out the Unicode code point for the character (from http://www.unicode.org) and then use ChrW to convert from the code point to the character. (To put this in another string, use concatenation. I'm somewhat surprised that VB doesn't have an escape sequence, but there we go.)
For example, for the Euro sign (U+20AC) you'd write:
Dim euro as Char = ChrW(&H20AC)
The advantage of this over putting the character directly into source code is that your source code stays "just pure ASCII" - which means you won't have any strange issues with any other program trying to read it, diff it, etc. The disadvantage is that it's harder to see the symbol in the code, of course.
The most common way seems to be to append a character of the form Chr(34)... 34 represents a double quote character. The character codes can be found from the windows program "charmap"... just windows/Run... and type charmap
If you are passing strings to be processed as SQL statement try doubling the characters for example.
"SELECT * FROM MyRecords WHERE MyRecords.MyKeyField = ""With a "" Quote"" "
The '' double works with the other special characters as well.
The ' character can be doubled up to allow it into a string e.g
lSQLSTatement = "Select * from temp where name = 'fred''s'"
Will search for all records where name = fred's
Three points:
1) The example characters you've given are not special characters. They're directly available on your keyboard. Just press the corresponding key.
2) To type characters that don't have a corresponding key on the keyboard, use this:
Alt + (the ASCII code number of the special character)
For example, to type ¿, press Alt and key in 168, which is the ASCII code for that special character.
You can use this method to type a special character in practically any program not just a VB.Net text editor.
3) What you probably looking for is what is called 'escaping' characters in a string. In your SQL query string, just place a \ before each of those characters. That should do.
Chr() is probably the most popular.
ChrW() can be used if you want to generate unicode characters
The ControlChars class contains some special and 'invisible' characters, plus the quote - for example, ControlChars.Quote