How to Create a Symbol from a String That Has Whitespaces

How to create a symbol from a string that has whitespaces?

Try by yourself

"Lord of the rings".to_sym
#=> :"Lord of the rings"

How to correctly represent a whitespace character

Which whitespace character? The empty string is pretty unambiguous - it's a sequence of 0 characters. However, " ", "\t" and "\n" are all strings containing a single character which is characterized as whitespace.

If you just mean a space, use a space. If you mean some other whitespace character, there may well be a custom escape sequence for it (e.g. "\t" for tab) or you can use a Unicode escape sequence ("\uxxxx"). I would discourage you from including non-ASCII characters in your source code, particularly whitespace ones.

EDIT: Now that you've explained what you want to do (which should have been in your question to start with) you'd be better off using Regex.Split with a regular expression of \s which represents whitespace:

Regex regex = new Regex(@"\s");
string[] bits = regex.Split(text.ToLower());

See the Regex Character Classes documentation for more information on other character classes.

Is there something that looks like white space but it is not and how to remove it?

I have just tired to use ord(c) and I get 0 for the characters that I have interpreted as white spaces.

It indicates that the input data is utf-16 text. If zero bytes follow what appears to be ascii characters e.g., b'a\0' then it is 'utf-16le' (little-endian):

>>> b'd\0a\0t\0a\0'.decode('utf-16le')
u'data'

Don't use .replace(b'\0', b''); it will break on the first non-ascii character e.g., b'\xac ' (euro sign encoded using utf-16le character encoding).

What is the symbol for whitespace in C?

There is no particular symbol for whitespace. It is actually a set of few characters which are:

' '      space 
'\t' horizontal tab
'\n' newline
'\v' vertical tab
'\f' form feed
'\r' carriage return

Use isspace standard library function from ctype.h if you want to check for any of these white-spaces.

For just a space, use ' '.

Generate fixed length Strings filled with whitespaces

Since Java 1.5 we can use the method java.lang.String.format(String, Object...) and use printf like format.

The format string "%1$15s" do the job. Where 1$ indicates the argument index, s indicates that the argument is a String and 15 represents the minimal width of the String.
Putting it all together: "%1$15s".

For a general method we have:

public static String fixedLengthString(String string, int length) {
return String.format("%1$"+length+ "s", string);
}

Maybe someone can suggest another format string to fill the empty spaces with an specific character?

Check if a string has white space

You can simply use the indexOf method on the input string:

function hasWhiteSpace(s) {
return s.indexOf(' ') >= 0;
}

Or you can use the test method, on a simple RegEx:

function hasWhiteSpace(s) {
return /\s/g.test(s);
}

This will also check for other white space characters like Tab.

JavaScript split String with white space

You could split the string on the whitespace and then re-add it, since you know its in between every one of the entries.

var string = "text to split";
string = string.split(" ");
var stringArray = new Array();
for(var i =0; i < string.length; i++){
stringArray.push(string[i]);
if(i != string.length-1){
stringArray.push(" ");
}
}

Update: Removed trailing space.

Checking whitespace in a string (python)

def tt(w): 
if ' ' in w:
print 'space'
else:
print 'no space'

>> tt('b ')
>> space
>> tt('b b')
>> space
>> tt('bb')
>> no space

I am in train, sorry for not explaining.. cannot type much..

Check whether string has both white spaces or special characters

You can use this condition:

preg_match('/[^A-Z]/i',$tagName);

Removed \s since [^A-Z] also includes spaces.



Related Topics



Leave a reply



Submit