3

I have Simple Question regarding space define in string in string.xml file.

Let's say If we define string

<string name="regNumber">RegNumber : </string>

it will consider space before Colon not after. if we have put space after Colon then we have to define below way.

<string name="regNumber">RegNumber :\u0020</string>

or let's say this way also

<string name="regNumber">RegNumber :&#160;</string>

Question : why space is not Consider After Colon without writing any code ?

Brief Explanation :

If write this

<string name="regNumber">RegNumber : </string>

it will put space before Colon but not After the Colon

so the text will be RegNumber :

But My Question it will output like this it will not consider before Colon why it is consider.

RegNumber:

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • Possible duplicate of [How to keep the spaces at the end and/or at the beginning of a String?](http://stackoverflow.com/questions/1587056/how-to-keep-the-spaces-at-the-end-and-or-at-the-beginning-of-a-string) – Wiktor Stribiżew Nov 22 '16 at 10:43
  • *My question why is not consider as Space I want to set Space.* - because otherwise, it gets discarded. – Wiktor Stribiżew Nov 22 '16 at 10:45
  • @WiktorStribiżew I know when we write ` ` or `\u0020` it will add space but my question is why it is not consider `space` after `Colon` because before `Colon` it is consider. Now remove `flag` if you have clear Idea. – Harshad Pansuriya Nov 22 '16 at 10:46
  • The colon is not a whitespace and a whitespace before it is not a leading/trailing whitespace. All leading/trailing whitespace is removed. – Wiktor Stribiżew Nov 22 '16 at 10:47
  • @WiktorStribiżew you are not `clear` with my question. I am not say how to set , I am say that why is not consider after `Colon` but before `Colon` it is consider. – Harshad Pansuriya Nov 22 '16 at 10:49
  • @WiktorStribiżew check out again space is consider before `Colon`. – Harshad Pansuriya Nov 22 '16 at 10:53
  • If you add spaces at the last of a string then it will trim all the spaces[Not hard coded spaces] as you already mentioned in your result "RegNumber :". Before colon space doesn't removed because it is not at the last of the string. It's a basic logic of android, no need to worry about it. – Ready Android Nov 22 '16 at 11:20

2 Answers2

0

Because by default, android trim all the String to remove unnecessary spaces.. Say you accidentally pressed space bar and there were 20 spaces in your string file now,

<string name="regNumber">RegNumber :                           </string>

Android just trim the leading and trailing spaces. I never find any reason behind it. But I believe, it's useful to validate the string using Regex. [PS: I might be wrong]

See also: Android's documentation on formatting string

O_o
  • 1,103
  • 11
  • 36
0

The regex defined is constructed to ignore white spaces at the beginning, and at the end. It is defined in the first phase of compiler building process called Lexical analysis.

Aleksandar G
  • 1,163
  • 2
  • 20
  • 25