< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page
rev 51726 : 8202442: String::unescape
Reviewed-by: smarks, rriggs, sherman


2955      *
2956      * @return string with margins removed, indentation adjusted and
2957      *         line terminators normalized
2958      *
2959      * @see String#align()
2960      *
2961      * @since 12
2962      */
2963     public String align(int n) {
2964         if (isEmpty()) {
2965             return "";
2966         }
2967         int outdent = lines().filter(not(String::isBlank))
2968                              .mapToInt(String::indexOfNonWhitespace)
2969                              .min()
2970                              .orElse(0);
2971         return indent(n - outdent, true);
2972     }
2973 
2974     /**


















































































2975      * This object (which is already a string!) is itself returned.
2976      *
2977      * @return  the string itself.
2978      */
2979     public String toString() {
2980         return this;
2981     }
2982 
2983     /**
2984      * Returns a stream of {@code int} zero-extending the {@code char} values
2985      * from this sequence.  Any char which maps to a <a
2986      * href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate code
2987      * point</a> is passed through uninterpreted.
2988      *
2989      * @return an IntStream of char values from this sequence
2990      * @since 9
2991      */
2992     @Override
2993     public IntStream chars() {
2994         return StreamSupport.intStream(




2955      *
2956      * @return string with margins removed, indentation adjusted and
2957      *         line terminators normalized
2958      *
2959      * @see String#align()
2960      *
2961      * @since 12
2962      */
2963     public String align(int n) {
2964         if (isEmpty()) {
2965             return "";
2966         }
2967         int outdent = lines().filter(not(String::isBlank))
2968                              .mapToInt(String::indexOfNonWhitespace)
2969                              .min()
2970                              .orElse(0);
2971         return indent(n - outdent, true);
2972     }
2973 
2974     /**
2975      * Translates all Unicode escapes and escape sequences in this string into
2976      * characters represented by those escapes specified in sections 3.3 and
2977      * 3.10.6 of the <cite>The Java&trade; Language Specification</cite>.
2978      * <p>
2979      * Each unicode escape in the form \unnnn is translated to the
2980      * unicode character whose code point is {@code 0xnnnn}. Care should be
2981      * taken when using UTF-16 surrogate pairs to ensure that the high
2982      * surrogate (U+D800..U+DBFF) is immediately followed by a low surrogate
2983      * (U+DC00..U+DFFF) otherwise a
2984      * {@link java.nio.charset.CharacterCodingException} may occur during UTF-8
2985      * decoding.
2986      * <p>
2987      * Backslash escape sequences are translated as follows;
2988      * <table class="plain">
2989      *   <caption style="display:none">Escape sequences</caption>
2990      *   <thead>
2991      *   <tr>
2992      *     <th scope="col">Escape</th>
2993      *     <th scope="col">Name</th>
2994      *     <th scope="col">Unicode</th>
2995      *   </tr>
2996      *   </thead>
2997      *   <tr>
2998      *     <td>{@code \b}</td>
2999      *     <td>backspace</td>
3000      *     <td>U+0008</td>
3001      *   </tr>
3002      *   <tr>
3003      *     <td>{@code \t}</td>
3004      *     <td>horizontal tab</td>
3005      *     <td>U+0009</td>
3006      *   </tr>
3007      *   <tr>
3008      *     <td>{@code \n}</td>
3009      *     <td>line feed</td>
3010      *     <td>U+000A</td>
3011      *   </tr>
3012      *   <tr>
3013      *     <td>{@code \f}</td>
3014      *     <td>form feed</td>
3015      *     <td>U+000C</td>
3016      *   </tr>
3017      *   <tr>
3018      *     <td>{@code \r}</td>
3019      *     <td>carriage return</td>
3020      *     <td>U+000D</td>
3021      *   </tr>
3022      *   <tr>
3023      *     <td>{@code \"}</td>
3024      *     <td>double quote</td>
3025      *     <td>U+0022</td>
3026      *   </tr>
3027      *   <tr>
3028      *     <td>{@code \'}</td>
3029      *     <td>single quote</td>
3030      *     <td>U+0027</td>
3031      *   </tr>
3032      *   <tr>
3033      *     <td>{@code \\}</td>
3034      *     <td>backslash</td>
3035      *     <td>U+005C</td>
3036      *   </tr>
3037      * </table>
3038      * <p>
3039      * Octal escapes {@code \0 - \377} are translated to their code
3040      * point equivalents.
3041      *
3042      * @return String with all escapes translated.
3043      *
3044      * @throws IllegalArgumentException when escape sequence is malformed.
3045      *
3046      * @since 12
3047      *
3048      * @deprecated  Preview feature associated with Raw String Literals.
3049      *              Use at your own risk.
3050      */
3051     @Deprecated(forRemoval=true, since="12")
3052     public String unescape() throws IllegalArgumentException {
3053         return isLatin1() ? StringLatin1.unescape(value) : StringUTF16.unescape(value);
3054     }
3055 
3056     /**
3057      * This object (which is already a string!) is itself returned.
3058      *
3059      * @return  the string itself.
3060      */
3061     public String toString() {
3062         return this;
3063     }
3064 
3065     /**
3066      * Returns a stream of {@code int} zero-extending the {@code char} values
3067      * from this sequence.  Any char which maps to a <a
3068      * href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate code
3069      * point</a> is passed through uninterpreted.
3070      *
3071      * @return an IntStream of char values from this sequence
3072      * @since 9
3073      */
3074     @Override
3075     public IntStream chars() {
3076         return StreamSupport.intStream(


< prev index next >