Get the actual length of a specific string in Java

I defined String as

String s = "\\"; int length = s.length(); // returns 1 System.out.println(s); // prints only one "\" 

How can I get a size equal to 2 ?

UPD: The problem is not getting exactly 2-dimensional row. I need to get the number of source characters that I defined for my string.

+5
source share
4 answers

Use CharConverter from DrJava . You can adapt the source code for your project. It has a method that converts all escaped characters into a string back to real Java input.

 String str1 = "\\"; String str2 = CharConverter.escapeString(str1); System.out.println(str2.length()); // prints 2 
+2
source

String s = "\\"; contains only the \ character, and since it is special, it must be escaped with the \ character.

To get a 2-dimensional string, you can avoid two backslashes, for example:

 String s = "\\\\"; 

This size does not have a size of 4 , but 2 , because there are characters (obviously, like a backslash) that are not represented by a single visual element in the editor.

There are also characters that can be completely invisible when printed (for example, the Mongolian vowel separator ), but which are represented in a different way in the source (by their Unicode code). For example, the Mongolian vowel separator can be represented as:

 String mongolianVowelSeparator = "\u180"; <-- one character only, invisible when printed 

So, here we have only one character ( U+180E Unicode character), but we used five editor characters to represent it.

+10
source

See JLS - 3.10.6. Escape sequences for characters and string literals :

Character and string escape sequences allow the representation of certain non-graphic characters, as well as single quotes, double quotes, and backslash characters in character literals ( ยง3.10.4 ) and string literals ( ยง3.10.5 ).

...

\ \ /* \u005c: backslash \ */

The \ character is used in Unicode Escapes :

In addition to the processing implied by the grammar, for each raw input character that is a backslash \ , input processing should take into account how many other characters \ adjacent precede it, separating it from the non \ or beginning of the input stream. If this number is even, then \ has the right to start a Unicode escape; if the number is odd, then \ does not have the right to run Unicode-escape.

It is worth noting that your string can be written as:

 \u005c 

Since 005c is the Unicode value for \ .

+2
source

As indicated above, "\" is an escape character and does not stack to the total length of the string. However, if you insist that escape characters be added to the total length, try this code snippet below.

For each "\" in your lines, the total length is increased by one.

 String s = "\\"; int length = s.length(); for(int i = 0; i < s.length; i++) if(s.charAt(i) == '\') length++; 
-2
source

Source: https://habr.com/ru/post/1238820/


All Articles