NSNonLossyASCIIStringEncoding equivalent for Android

前端 未结 3 734
梦毁少年i
梦毁少年i 2021-01-06 17:23

I got to port some chat code from iOS to Android. Before sending the chat message to the socket, the iOS code uses the NSNonLossyASCIIStringEncoding class as pa

3条回答
  •  萌比男神i
    2021-01-06 17:58

    My answer code is equivalent to IOS NSNonLossyASCIIStringEncoding for Android.

    In your gradle put below depandancy.

     compile 'org.apache.commons:commons-lang3:3.4'
    

    then Put method to your Utils Class Like this

     public static String encode(String s)
    {
        return StringEscapeUtils.escapeJava(s);
    
    }
    
    public static String decode(String s)
    {
        return StringEscapeUtils.unescapeJava(s);
    
    }
    

    then Simply call this method where you want to encode string or decode String like this

    //for encode
    String stencode = Utils.encode("mystring");
    
    
    //for decode
    String stdecode = Utils.decode("mystring")
    

提交回复
热议问题