关于string的一些细节

String有length()方法,数组有length属性

String的长度实际上就是它的属性--char型数组value的长度。数组是没有length()方法的,大家知道,在JAVA中,数组也被作为对象来处理,它的方法都继承自Object类。数组有一个属性length,这也是它唯一的属性,对于所有类型的数组都是这样。

在JAVA中,一个char是2个字节(byte),而一个中文汉字是一个字符,也是2个字节。所以可以把汉字赋值给char。而英文字母都是一个字节的,因此它也能保存到一个byte里,一个中文汉字却不能。

char型字符单独在输出语句时,输出它的字符本身,与+相连时,输出它的ASCII码值。

UTF-16BE和UTF-16LE是UNICODE编码家族的两个成员。UNICODE标准定义了UTF-8、UTF-16、UTF-32三种编码格式,共有UTF-8、UTF-16、UTF-16BE、UTF-16LE、UTF-32、UTF-32BE、UTF-32LE七种编码方案。JAVA所采用的编码方案是UTF-16BE。

substring(int beginIndex, int endIndex)方法,是按字符截取的,不是按字节。一个汉字和字母都看作一个字符。

 

字符串的反转输出的简单方法:

  1. public class StringReverse {   
  2.     public static void main(String[] args) {   
  3.         // 原始字符串   
  4.         String "A quick brown fox jumps over the lazy dog.";   
  5.         System.out.println("原始的字符串:" s);   
  6.   
  7.         System.out.print("反转后字符串:");   
  8.         StringBuffer buff new StringBuffer(s);   
  9.         // java.lang.StringBuffer类的reverse()方法可以将字符串反转   
  10.         System.out.println(buff.reverse().toString());   
  11.     }   
  12.  

运行结果:

  1. 原始的字符串:A quick brown fox jumps over the lazy dog.
  2. 反转后字符串:.god yzal eht revo spmuj xof nworb kciuq A

    

字符编码例: 

  1. import java.io.UnsupportedEncodingException  
  2. public class EncodeTest {   
  3.       
  4.     public static void printByteLength(String s, String encodingName) {   
  5.         System.out.print("字节数:");   
  6.         try {   
  7.             System.out.print(s.getBytes(encodingName).length);   
  8.         catch (UnsupportedEncodingException e) {   
  9.             e.printStackTrace();   
  10.         }   
  11.         System.out.println(";编码:" encodingName);   
  12.     }   
  13.   
  14.     public static void main(String[] args) {   
  15.         String en "A";   
  16.         String ch "人";   
  17.   
  18.         // 计算一个英文字母在各种编码下的字节数   
  19.         System.out.println("英文字母:" en);   
  20.         EncodeTest.printByteLength(en, "GB2312");   
  21.         EncodeTest.printByteLength(en, "GBK");   
  22.         EncodeTest.printByteLength(en, "GB18030");   
  23.         EncodeTest.printByteLength(en, "ISO-8859-1");   
  24.         EncodeTest.printByteLength(en, "UTF-8");   
  25.         EncodeTest.printByteLength(en, "UTF-16");   
  26.         EncodeTest.printByteLength(en, "UTF-16BE");   
  27.         EncodeTest.printByteLength(en, "UTF-16LE");   
  28.   
  29.         System.out.println();   
  30.   
  31.         // 计算一个中文汉字在各种编码下的字节数   
  32.         System.out.println("中文汉字:" ch);   
  33.         EncodeTest.printByteLength(ch, "GB2312");   
  34.         EncodeTest.printByteLength(ch, "GBK");   
  35.         EncodeTest.printByteLength(ch, "GB18030");   
  36.         EncodeTest.printByteLength(ch, "ISO-8859-1");   
  37.         EncodeTest.printByteLength(ch, "UTF-8");   
  38.         EncodeTest.printByteLength(ch, "UTF-16");   
  39.         EncodeTest.printByteLength(ch, "UTF-16BE");   
  40.         EncodeTest.printByteLength(ch, "UTF-16LE");   
  41.     }   
  42.  

运行结果如下:

  1. 英文字母:A
  2. 字节数:1;编码:GB2312
  3. 字节数:1;编码:GBK
  4. 字节数:1;编码:GB18030
  5. 字节数:1;编码:ISO-8859-1
  6. 字节数:1;编码:UTF-8
  7. 字节数:4;编码:UTF-16
  8. 字节数:2;编码:UTF-16BE
  9. 字节数:2;编码:UTF-16LE
  10. 中文汉字:人
  11. 字节数:2;编码:GB2312
  12. 字节数:2;编码:GBK
  13. 字节数:2;编码:GB18030
  14. 字节数:1;编码:ISO-8859-1
  15. 字节数:3;编码:UTF-8
  16. 字节数:4;编码:UTF-16
  17. 字节数:2;编码:UTF-16BE
  18. 字节数:2;编码:UTF-16LE

字符截取例:

  1. import java.io.UnsupportedEncodingException;      
  2.      
  3. public class CutString      
  4.     public static void main(String[] args) throws UnsupportedEncodingException      
  5.         String "我ZWR爱JAVA"     
  6.         // 获取GBK编码下的字节数据      
  7.         byte[] data s.getBytes("GBK");      
  8.         byte[] tmp new byte[6];      
  9.         // 将data数组的前六个字节拷贝到tmp数组中      
  10.         System.arraycopy(data, 0tmp, 06);      
  11.         // 将截取到的前六个字节以字符串形式输出到控制台      
  12.         new String(tmp);      
  13.         System.out.println(s);      
  14.          

输出结果:

  1. 我ZWR?

例2:

  1. import java.io.UnsupportedEncodingException;   
  2.   
  3. public class CutString {   
  4.   
  5.       
  6.     public static boolean isChineseChar(char c)   
  7.             throws UnsupportedEncodingException {   
  8.         // 如果字节数大于1,是汉字   
  9.         // 以这种方式区别英文字母和中文汉字并不是十分严谨,但在这个题目中,这样判断已经足够了  
  10.         return String.valueOf(c).getBytes("GBK").length 1;   
  11.     }   
  12.   
  13.       
  14.     public static String substring(String orignal, int count)   
  15.             throws UnsupportedEncodingException {   
  16.         // 原始字符不为null,也不是空字符串   
  17.         if (orignal != null && !"".equals(orignal)) {   
  18.             // 将原始字符串转换为GBK编码格式   
  19.             orignal new String(orignal.getBytes(), "GBK");   
  20.             // 要截取的字节数大于0,且小于原始字符串的字节数   
  21.             if (count 0 && count orignal.getBytes("GBK").length) {   
  22.                 StringBuffer buff new StringBuffer();   
  23.                 char c;   
  24.                 for (int 0count; i++) {   
  25.                     // charAt(int index)也是按照字符来分解字符串的   
  26.                     orignal.charAt(i);   
  27.                     buff.append(c);   
  28.                     if (CutString.isChineseChar(c)) {   
  29.                         // 遇到中文汉字,截取字节总数减1   
  30.                         --count;   
  31.                     }   
  32.                 }   
  33.                 return buff.toString();   
  34.             }   
  35.         }   
  36.         return orignal;   
  37.     }   
  38.   
  39.     public static void main(String[] args) {   
  40.         // 原始字符串   
  41.         String "我ZWR爱JAVA";   
  42.         System.out.println("原始字符串:" s);   
  43.         try {   
  44.             System.out.println("截取前1位:" CutString.substring(s, 1));   
  45.             System.out.println("截取前2位:" CutString.substring(s, 2));   
  46.             System.out.println("截取前4位:" CutString.substring(s, 4));   
  47.             System.out.println("截取前6位:" CutString.substring(s, 6));   
  48.         catch (UnsupportedEncodingException e) {   
  49.             e.printStackTrace();   
  50.         }   
  51.     }   
  52.  

运行结果:

  1. 原始字符串:我ZWR爱JAVA
  2. 截取前1位:我
  3. 截取前2位:我
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`string.format`是C++中的一个函数,用于格式化字符串。它的使用方法如下: ```cpp #include <iostream> #include <string> int main() { std::string formattedString = string_format("My name is %s and I am %d years old.", "John", 25); std::cout << formattedString << std::endl; return 0; } ``` 在上面的示例中,我们使用`string.format`函数将一个字符串格式化为另一个字符串。`%s`和`%d`是占位符,用于表示将要被替换的值的位置。在这里,`%s`会被替换为字符串参数"John",`%d`会被替换为整数参数25。 在C++中,使用`%`字符作为占位符的起始标志,后面跟着一个字母来表示不同的数据类型。常见的占位符及其对应的数据类型如下: - `%d`:用于整数类型(int、long等)的占位符。 - `%f`:用于浮点数类型(float、double等)的占位符。 - `%s`:用于字符串类型(char*、std::string等)的占位符。 - `%c`:用于字符类型的占位符。 除了占位符,还可以在格式字符串中添加其他文本,如示例中的"My name is"和"and I am"。这些文本会原样输出到最终的格式化字符串中。 需要注意的是,如果格式字符串中的占位符数量与提供的参数数量不匹配,会导致未定义的行为或错误。因此,在使用`string.format`时,确保提供的参数数量和占位符数量一致。 此外,C++中还有其他更灵活和强大的字符串格式化函数,如`printf`和`std::stringstream`。这些函数提供更多的格式控制选项和功能,可以根据具体需求选择合适的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值