字符相关



1
2
3
4
5
String str =  "今天天气不错" ;
int  index = str.indexOf( "天气" );
System.out.println(index);    // 大于0 则表示存在 为-1 则表示不存在
String s = str.replace( "天气" "心情" );
System.out.println(s);        // 输出“今天心情不错
我是一条分割线

网页中文字符编码转换

String s="abc";
String s1=URLEncoder.encode(s, "utf-8");

我是一条分割线

int -> String

int i=12345;
String s="";
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?

String -> int

s="12345";
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?

以下是答案:

第一种方法:s=i+"";   //会产生两个String对象
第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象

第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常
第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象

--------------------------------------------------------------------
1如何将字串 String 转换成整数 int?

A. 有两个方法:

1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

注: 字串转成 Double, Float, Long 的方法大同小异.
2 如何将整数 int 转换成字串 String ?
A. 有叁种方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = "" + i;

注: Double, Float, Long 转成字串的方法大同小异.

我是一条分割线


      /**
      * 组合JSON  转换成String
      *
      * @param s
      * @param ss
      * @throws JSONException
      */
      public void putJSON(String s , String ss ) throws JSONException {
           int a = s .indexOf( ":" );
          String friendName = s .substring(0, a );
          String problem = s .substring( a + 2);
           try {

              JSONObject lan = new JSONObject();
               lan .put( "Friend name" , friendName );
               lan .put( friendName , ss );
               id = id + 1;
              System. out .println( "封装的JSON:" + lan );

               char [] stack = new char [1024];
               int top = -1;
              String str = lan .toString();
              StringBuffer sb = new StringBuffer();
               char [] charArray = str .toCharArray();
               for ( int i = 0; i < charArray . length ; i ++) {
                    char c = charArray [ i ];
                    if ( '{' == c ) {
                         stack [++ top ] = c ;
                         sb .append( charArray [ i ] + "\n" );
                         for ( int j = 0; j <= top ; j ++) {
                              sb .append( "\t" );
                        }
                         continue ;
                   }
                    if (( i + 1) <= ( charArray . length - 1)) {
                         char d = charArray [ i + 1];
                         if ( '}' == d ) {
                              top --; // 将数组的最后一个有效内容位置下标减 1,可以简单的理解为将栈顶数据弹出
                              sb .append( charArray [ i ] + "\n" );
                              for ( int j = 0; j <= top ; j ++) {
                                   sb .append( "\t" );
                             }
                              continue ;
                        }
                   }

                    if ( ',' == c ) {
                         sb .append( charArray [ i ] + "\n" );
                         for ( int j = 0; j <= top ; j ++) {
                              sb .append( "\t" );
                        }
                         continue ;
                   }
                    sb .append( c );

              }
              System. out .println( sb );
              String con = sb .toString();
              putRun( con );
          } catch (Exception e ) {
               // TODO : handle exception
          }
     }

我是一条分割线



public class StringTest {
    private static String hexString = "0123456789ABCDEFabcdef" ;
    public static void main(String[] args ) {
        String msg = "412ee69e9ce985b120e794b5e5ad90e7839fe5bab7e8af9ae4b880e59381e68b9be4bba3e79086" ;
//        System.out.println(encode(msg));
        System. out .println(decode( msg ));
    }
 
    public static String encode(String str ) {    //中文转换hex编码
        byte [] bytes = str .getBytes();
        StringBuilder sb = new StringBuilder( bytes . length * 2);
        //转换hex编码
        for ( byte b : bytes ) {
            sb .append(Integer.toHexString( b + 0x800).substring(1));
        }
        str = sb .toString();
        return str ;
    }
    //把hex编码转换为string
    public static String decode(String bytes ) {
        bytes = bytes .toUpperCase();
        ByteArrayOutputStream baos = new ByteArrayOutputStream( bytes .length() / 2);
        // 将每2位16进制整数组装成一个字节
        for ( int i = 0; i < bytes .length(); i += 2)
            baos .write(( hexString .indexOf( bytes .charAt( i )) << 4 | hexString .indexOf( bytes .charAt( i + 1))));
        return new String( baos .toByteArray());
    }
}

    我是一条你看不见的分割线

url [ j ] = url [ j ].replaceAll( "\\" , "" );
将\替换为空   JAVA中 \\代表一个\     replaceAll替换所有


    我是一条你看不见的分割线
分割字符串
public  static  void  main(String[] args) {
         String s =  "ab cde fg" ;
         String[] re = s.split( " " ); //用split()函数直接分割
         for  (String string : re) {
             System.out.println(string);
         }
     }

    我是一条你看不见的分割线
1
2
3
4
5
String str =  "今天天气不错" ;
int  index = str.indexOf( "天气" );
System.out.println(index);    // 大于0 则表示存在 为-1 则表示不存在
String s = str.replace( "天气" "心情" );
System.out.println(s);        // 输出“今天心情不错
我是一条分割线

网页中文字符编码转换

String s="abc";
String s1=URLEncoder.encode(s, "utf-8");

我是一条分割线

int -> String

int i=12345;
String s="";
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?

String -> int

s="12345";
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?

以下是答案:

第一种方法:s=i+"";   //会产生两个String对象
第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象

第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常
第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象

--------------------------------------------------------------------
1如何将字串 String 转换成整数 int?

A. 有两个方法:

1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

注: 字串转成 Double, Float, Long 的方法大同小异.
2 如何将整数 int 转换成字串 String ?
A. 有叁种方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = "" + i;

注: Double, Float, Long 转成字串的方法大同小异.

我是一条分割线


      /**
      * 组合JSON  转换成String
      *
      * @param s
      * @param ss
      * @throws JSONException
      */
      public void putJSON(String s , String ss ) throws JSONException {
           int a = s .indexOf( ":" );
          String friendName = s .substring(0, a );
          String problem = s .substring( a + 2);
           try {

              JSONObject lan = new JSONObject();
               lan .put( "Friend name" , friendName );
               lan .put( friendName , ss );
               id = id + 1;
              System. out .println( "封装的JSON:" + lan );

               char [] stack = new char [1024];
               int top = -1;
              String str = lan .toString();
              StringBuffer sb = new StringBuffer();
               char [] charArray = str .toCharArray();
               for ( int i = 0; i < charArray . length ; i ++) {
                    char c = charArray [ i ];
                    if ( '{' == c ) {
                         stack [++ top ] = c ;
                         sb .append( charArray [ i ] + "\n" );
                         for ( int j = 0; j <= top ; j ++) {
                              sb .append( "\t" );
                        }
                         continue ;
                   }
                    if (( i + 1) <= ( charArray . length - 1)) {
                         char d = charArray [ i + 1];
                         if ( '}' == d ) {
                              top --; // 将数组的最后一个有效内容位置下标减 1,可以简单的理解为将栈顶数据弹出
                              sb .append( charArray [ i ] + "\n" );
                              for ( int j = 0; j <= top ; j ++) {
                                   sb .append( "\t" );
                             }
                              continue ;
                        }
                   }

                    if ( ',' == c ) {
                         sb .append( charArray [ i ] + "\n" );
                         for ( int j = 0; j <= top ; j ++) {
                              sb .append( "\t" );
                        }
                         continue ;
                   }
                    sb .append( c );

              }
              System. out .println( sb );
              String con = sb .toString();
              putRun( con );
          } catch (Exception e ) {
               // TODO : handle exception
          }
     }

我是一条分割线



public class StringTest {
    private static String hexString = "0123456789ABCDEFabcdef" ;
    public static void main(String[] args ) {
        String msg = "412ee69e9ce985b120e794b5e5ad90e7839fe5bab7e8af9ae4b880e59381e68b9be4bba3e79086" ;
//        System.out.println(encode(msg));
        System. out .println(decode( msg ));
    }
 
    public static String encode(String str ) {    //中文转换hex编码
        byte [] bytes = str .getBytes();
        StringBuilder sb = new StringBuilder( bytes . length * 2);
        //转换hex编码
        for ( byte b : bytes ) {
            sb .append(Integer.toHexString( b + 0x800).substring(1));
        }
        str = sb .toString();
        return str ;
    }
    //把hex编码转换为string
    public static String decode(String bytes ) {
        bytes = bytes .toUpperCase();
        ByteArrayOutputStream baos = new ByteArrayOutputStream( bytes .length() / 2);
        // 将每2位16进制整数组装成一个字节
        for ( int i = 0; i < bytes .length(); i += 2)
            baos .write(( hexString .indexOf( bytes .charAt( i )) << 4 | hexString .indexOf( bytes .charAt( i + 1))));
        return new String( baos .toByteArray());
    }
}

    我是一条你看不见的分割线

url [ j ] = url [ j ].replaceAll( "\\" , "" );
将\替换为空   JAVA中 \\代表一个\     replaceAll替换所有


    我是一条你看不见的分割线
分割字符串
public  static  void  main(String[] args) {
         String s =  "ab cde fg" ;
         String[] re = s.split( " " ); //用split()函数直接分割
         for  (String string : re) {
             System.out.println(string);
         }
     }

    我是一条你看不见的分割线
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值