Groovy MissingMethodException when working with UTF-8 - utf-8

Currently I have the following String:
String str = "Hello my name\n\t\t\t\tis Earl."
The problem is that the remote process that handles this String doesn't like the character encoding of the newline and tab characters. This remote process expects UTF-8.
So I wrote convertSpecCharsToUtf8() method:
private String convertSpecCharsToUtf8() {
// "\n\t\t\t\t" as UTF-8
char[] utf8 = new char[6]
char[0] = '\\u000D'
char[1] = '\\u000A'
char[2] = char[3] = char[4] = char[5] = '\\u0009'
new String(utf8)
}
And then changed my str String to:
String str = "Hello my name" + convertSpecCharsToUtf8() + "is Earl."
When I run:
println "Testing UTF8"
String str = "Hello my name" + utf8CRLFTabFormat() + "is Earl."
println str
I get:
Testing UTF8
Caught: groovy.lang.MissingMethodException: No signature of method: static char.putAt() is applicable for argument types: (java.lang.Integer, java.lang.String) values: [0, \u000D]
groovy.lang.MissingMethodException: No signature of method: static char.putAt() is applicable for argument types: (java.lang.Integer, java.lang.String) values: [0, \u000D]
at com.me.myapp.convertSpecCharsToUtf8(Widget.groovy:133)
at com.me.myapp.execute(Widget.groovy:111)
at com.me.myapp$execute.call(Unknown Source)
at com.me.myapp.main(Widget.groovy:37)
Why, and what's the solution here?

There's a typo. Should be:
private String convertSpecCharsToUtf8() {
// "\n\t\t\t\t" as UTF-8
char[] utf8 = new char[6]
utf8[0] = '\\u000D'.toCharacter()
utf8[1] = '\\u000A'.toCharacter()
utf8[2] = utf8[3] = utf8[4] = utf8[5] = '\\u0009'.toCharacter()
new String(utf8)
}

You can write a list with each character and use the as operator to coerce to char[]. You may also use /str/ string declaration to avoid double escaping the backslash:
String convertSpecCharsToUtf8() {
new String( [/\u000D/, /\u000A/] + [/\u0009/] * 4 as char[] )
}
def str = "Hello my name" + convertSpecCharsToUtf8() + "is Earl."
assert str == """Hello my name
is Earl."""

Related

Swift 1.2, capture character from a word

My problem is how to get character from a word
The result I needed is
DisplayChar("asd",1)
and it will display "a"
func DisplayChar(word : String, number : Int) -> String{
let i: Int = count(word)
var result = 0
result = i - (i - number)
var str = ""
var j = 0
for j = 0; j < result; j++ {
str = str + word[j]
}
return str
}
DisplayChar("xyz", 2)
This code should work
let sentence = "Hello world"
let characters = Array(sentence)
print(characters[0]) // "H"
There are a couple good solutions in this answer that may work, two good ones duplicated below.
Convert to Array
let word = "test"
var firstChar = Array(word)[0] // t
(Note: this assumes a UTF8 or ASCII encoded string, but that is likely fine for school.)
Create Your Own Extension
First an extension of String to handle subscripts:
extension String {
subscript (i: Int) -> Character {
return self[self.startIndex.advancedBy(i)]
}
subscript (i: Int) -> String {
return String(self[i] as Character)
}
subscript (r: Range<Int>) -> String {
let start = startIndex.advancedBy(r.startIndex)
let end = start.advancedBy(r.endIndex - r.startIndex)
return self[Range(start ..< end)]
}
}
Then you can just use:
let word = "test"
var firstChar = word[0] // t
Swift strings have a method called substringToIndex, "asd".substringToIndex(1) will return "a".
I'm not sure if it works on Swift 1.2, though.

xamarin camera - No overload for method 'Exists' takes '1' arguments

I am trying to use the camera in Xamarin and have a method that gets a unique path as follows
private string GetUniquePath(string path, string name)
{
string ext = Path.GetExtension(name);
if (ext == string.Empty)
ext = ".jpg";
name = Path.GetFileNameWithoutExtension(name);
string newName = name + ext;
int i=1;
while (File.Exists(Path.Combine(path,newName)))
newName = name + "_" + (i++) + ext;
return Path.Combine(path, newName);
}
I get an error with -- while (File.Exists(Path.Combine(path,newName)))
The error is no overload for method 'Exists' takes 1 argument.
Yet thats the same format I see everywhere. any suggestions?
Error CS1501: No overload for method Exists' takes1' arguments
I would assume that it is picking up Java.IO.File.Exists which does not take any arguments.
Try fully qualifying the namespaces:
while (System.IO.File.Exists(System.IO.Path.Combine(path,newName)))
newName = name + "_" + (i++) + ext;

Why Kotlin doesn't implement Int.plus(value: String)?

It causes discomfort when you can do that:
val string = " abc "
val integer = 8
val result = string + integer
and can't do:
val result = integer + string
It has hidden meaning or it's an omission?
Kotlin is static typed language and in basicly you can't add String to Integer. But there are possible to overload operators, so we can now.
In case when we want add any object to string, it's clear: every object can be implicitly converted to String (Any#toString())
But in case of Int + smthg it's not so clear, so only Int + kotlin.Number is defined in standard library.
I suggest to use string interpolation:
val result = "${integer}${string}"
Or define own overloaded plus operator:
operator fun Int.plus(string: String): String = string + this

Processing: create an array of the characters within the string

I am new to processing and trying to figure out a way to create an array of all the characters within a string. Currently I Have:
String[] words = {"hello", "devak", "road", "duck", "face"};
String theWord = words[int(random(0,words.length))];
I've been googling and haven't found a good solution yet. Thanks in advance.
In addition to the comment you posted (which perhaps should have been an answer), there are a ton of ways to split a String.
The most obvious solution might be the String.split() function. If you give that function an empty String "" as an argument, it will split every character:
void setup() {
String myString = "testing testing 123";
String[] chars = myString.split("");
for (String c : chars) {
println(c);
}
}
You could also just use the String.charAt() function:
void setup() {
String myString = "testing testing 123";
for (int i = 0; i < myString.length(); i++) {
char c = myString.charAt(i);
println(c);
}
}

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