Processing: replace space with non-breaking space - char

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);

Related

Why does an error occur when I try to test if a member variable (string) in a class is empty?

I'm working on a project and need to test one of my class' member variables to verify that the user did indeed enter a string.
I've also tried using (patronName == '') and (patronName == "") but have had no luck.
Edit: Using "\n" fixes the error but the program ends without allowing the user to enter a name.
std::string Restaurant::getPatronName()
{
bool controlFlag = true;
do
{
getline(std::cin,patronName);
if ((std::cin.fail()) || (patronName == '\n'))
{
std::cout << "You must enter a name!" << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else
{
controlFlag = false;
}
} while (controlFlag);
return patronName;
}
The function should read and store the name entered by the user into patronName. When trying to build, I get an error that says "no match for 'operator=='". Could this be because the object called in main is a pointer of type Restaurant?
Besides the type mismatch between the character '\n' and the std::string patronName, we can find at https://en.cppreference.com/w/cpp/string/basic_string/getline that std::getline(input, str, delim);
Extracts characters from input and appends them to str until […] the next available input character is delim, […], in which case the delimiter character is extracted from input, but is not appended to str.
So there won't be any '\n' character, if delim is the newline, in the first place.
You can use std::basic_string::empty() to check if a string is empty.
What happens with '\n' is you are comparing a string with a char, which, i suspect, there is no operator== defined for this case. If you are sure the string isn't empty, you can call operator[] formerName[0], which returns a char.
You have to write patronName == "\n" because you cannot compare string and character

Golang string end character

I'm a newbie in Golang, so I'm playing with some algorithms and i have a little problem.
In java for insert an end string in char array I can do like this:
String str = "Mr John Smith ";
char[] arr = str.toCharArray();
arr[12] = '\0';
But in Golang I'm trying like this:
str := []byte("Mr John Smith ")
str[12] = '\0'
But this code didn't work
That's not a valid syntax for a rune literal with a 0 value. You can use the hex escape sequence
str[12] = '\x00'
If you really need an octal value, it requires 3 digits
str[12] = '\000'
Or just assign a literal 0
str[12] = 0
You can see the valid rune literal escape sequences in the specification: https://golang.org/ref/spec#Rune_literals

Groovy MissingMethodException when working with 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."""

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

Remove the desired content from a text

I would like to get a working code to simply remove from a text line a specific part that always begins with "(" and finish with ")".
Sample text : Hello, how are you (it is a question)
I want to remove this part: "(it is a question)" to only keep this message "Hello, how are you"
Lost...
Thanks
One way using Regular Expressions;
input = "Hello, how are you (it is a question)"
dim re: set re = new regexp
with re
.pattern = "\(.*\)\s?" '//anything between () and if present 1 following whitespace
.global = true
input = re.Replace(input, "")
end with
msgbox input
If the part to be removed is always at the end of the string, string operations would work as well:
msg = "Hello, how are you (it is a question)"
pos = InStr(msg, "(")
If pos > 0 Then WScript.Echo Trim(Left(msg, pos-1))
If the sentence always ends with the ( ) section, use the split function:
line = "Hello, how are you (it is a question)"
splitter = split(line,"(") 'splitting the line into 2 sections, using ( as the divider
endStr = splitter(0) 'first section is index 0
MsgBox endStr 'Hello, how are you
If it is in the middle of the sentence, use the split function twice:
line = "Hello, how are you (it is a question) and further on"
splitter = split(line,"(")
strFirst = splitter(0) 'Hello, how are you
splitter1 = split(line,")")
strSecond = splitter1(UBound(Splitter1)) 'and further on
MsgBox strFirst & strSecond 'Hello, how are you and further on
If there is only one instance of "( )" then you could use a '1' in place of the UBound.
Multiple instances I would split the sentence and then break down each section containing the "( )" and concatenate the final sentence.

Resources