gbk utf-8 string java

String d = "汗d";
String e = "喊";
String f = "d";
System.out.println("String length is " + d.length() + ". " + d.getBytes("GBK").length
+ " bytes in GBK or " + d.getBytes("UTF-8").length
+ " bytes in UTF-8");

System.out.println("char array length is " + d.toCharArray().length);
System.out.println("String length is " + e.length() + ". " + e.getBytes("GBK").length
+ " bytes in GBK or " + e.getBytes("UTF-8").length
+ " bytes in UTF-8");
System.out.println("char array length is " + e.toCharArray().length);
System.out.println("String length is " +f.length() + ". " + f.getBytes("GBK").length
+ " bytes in GBK or " + f.getBytes("UTF-8").length
+ " bytes in UTF-8");
System.out.println("char array length is " + f.toCharArray().length);

System.out.println("default encoding is " +System.getProperty("file.encoding"));

//output

String length is 2. 3 bytes in GBK or 4 bytes in UTF-8
char array length is 2
String length is 1. 2 bytes in GBK or 3 bytes in UTF-8
char array length is 1
String length is 1. 1 bytes in GBK or 1 bytes in UTF-8
char array length is 1
default encoding is UTF-8

//output - 2

String length is 2. 3 bytes in GBK or 4 bytes in UTF-8
char array length is 2
String length is 1. 2 bytes in GBK or 3 bytes in UTF-8
char array length is 1
String length is 1. 1 bytes in GBK or 1 bytes in UTF-8
char array length is 1
default encoding is GBK

时间: 2025-01-15 03:09:54

gbk utf-8 string java的相关文章

Reverse Words in a String (JAVA)

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 1 public class Solution { 2 public String reverseWords(String s) { 3 if(s.equals(""))return s; 4 String

jsp必须为gbk编码时候,java代码中getParameter获得中文出现乱码解决方案

本人对java,jsp等相关网页只是很不熟悉,今日为了修改别人留下来的java后台html中嵌套jsp做界面的项目中的一些内容受尽了折磨.在text或者textarea中传入中文的时候,getParameter方法总是得到一堆?,无人可问,只好在网上盲目的找.网上有好多更改jsp编码的方法,也有很多在java中对编码进行各种转换的,试了又试,没什么用.或许可以成功吧,但是我的情况是,当前jsp引用了别的jsp,然而别的jsp从gbk变成utf-8之后就报了html-500的错误,为了不引起更多的

String java问题随笔

1.字符串加密 设计思想: 每个字符都能够转化为整数型,也能将整数型转化为字符类型,这样我们在加密时候由于向后推3个,所以可以将字符转换为整形,然后加3,之后在将运算完的变量转化为字符后输出,就可以实现字符串加密. 程序流程图: 输入字符串->将字符串分解为字符->字符转换为整形->整形加3->转化为字符->输出. 源代码:import java.util.*; public class zifu { public static void main(String args[]

Scanner、String(java基础知识十二)

1.Scanner的概述和方法介绍 * A:Scanner的概述 * 是一个从键盘输入的类,有final修饰,不能被子类继承 * Scanner sc = new Scanner(System.in); * String str = sc.nextLine(); * B:Scanner的构造方法原理 * Scanner(InputStream source) * System类下有一个静态的字段: * public static final InputStream in; 标准的输入流,对应着键

int string java 呼转

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(); 这两

String java

https://www.golinuxcloud.com/java-interview-questions-answers-experienced-2/ 75. What is the meaning of Immutable in the context of String class in Java? An Immutable object cannot be modified or changed in Java. String is an Immutable class in Java.

leetcode 151. Reverse Words in a String --------- java

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. 倒序字符串. 注意使用BufferString,因为如果使用Stri

【Leetcode】Reverse Words in a String JAVA实现

一.题目描述 Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 二.分析 要注意几点:1.当字符串的头部或者尾部存在空格时,最后都将被消除 2.当两个子字符串之间的空格的个数大于1时,只要保留一个 解题思路:1.首先,将整个字符串进行反转 2.然后,使用split函数对字符串

344. Reverse String Java Solutions

Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". Subscribe to see which companies asked this question 1 public class Solution { 2 public String reverseString(Str