C/C++的字符串和JAVA的String对象
来源:互联网 发布:java特殊字符转义 编辑:程序博客网 时间:2024/06/05 17:21
1.关于C/C++的字符串与JAVA中的String对象基础知识
①char str1[] = "hello world";
char str2[] = "hello world";
str1 != str2; //str1和str2是两个字符串数组,我们会为它们分别分配两个长度为12个字节的,他们是两个初始地址不同的数组,故str1和str2不同;
②char * str3 = "hello world";
char * str4 = "hello world"; //str3和str4是两个指向字符串常量的指针,字符串常量在内存中只有一份拷贝;
③String str5 = "hello world";
String str6 = "hello world"; //JAVA中str5和str6是指向同一个String常量的句柄,它们指向的是常量池中的同一个String常量。
④String str1 = "ab" + "cd";
String str2 = "abcd";
String str3 = "ab";
String str4 = str3 + "cd";
str1 == str2; //为什么地址相同?str1在创建时"ab"和"cd"都是常量,变量str1在编译时就确定了,这里我们只创建了一个"abcd"放在常量池中;
str1 != str4; //为什么地址不同?只有使用引号包含文本的方式创建的String对象之间使用+连接产生的新对象才会放入常量池中,new方式产生的String对象(包括null)使用+产生的新对象不会放入常量池中;
⑤public static final String A = "ab";
public static final String B = "cd";
String str1 = A + B;
String str2 = "abcd";
str1 == str2; //A B是常量,str1在编译时就确定了
⑥public static final String A;
public static final String B;
static{
A = "ab";
B = "cd";
}
String str1 = A + B;
String str2 = "abcd";
str1 != str2; //A B虽然被定义为常量,但是它们没有被马上赋值,str1在编译时不能确定;
⑦Sting对象在JVM中的存储,字符串池与堆和栈的关系
String类有一个value属性,保存着String对象的值,类型是char[],这也正说明了字符串就是字符的序列;当执行String a="abc";时,JAVA虚拟机会在栈中创建三个char型的值'a'、'b'和'c',然后在堆中创建一个String对象,它的值(value)是刚才在栈中创建的三个char型值组成的数组{'a','b','c'},最后这个新创建的String对象会被添加到字符串池中。如果我们接着执行String b=new String("abc");代码,由于"abc"已经被创建并保存于字符串池中,因此JAVA虚拟机只会在堆中新创建一个String对象,但是它的值(value)是共享前一行代码执行时在栈中创建的三个char型值'a'、'b'和'c'。
⑧String的length()方法:String对象在底层是char型数组,其length()方法返回的是数组的value长度;
⑨JAVA中一个char型变量的长度为2个字节,故它可以存储一个中文字符,但是一个byte变量就不能存储一个中文字符;
char a = '中';
char b = '国';
a + b != '中国'; //因为“+”运算符,当它被用于字符串和字符串之间,或者字符串和其他类型变量之间时,它产生的效果是字符串的拼接;但当它被用于字符 和字符之间时,效果等同于用于数字和数字之间,是一种算术运算。
2.C++替换空格
length为string字符串数组可以容纳的字符的数量
void replaceSpace(char string[],int length){if(string == null || length <= 0)return;int coutSpace = 0;int originalLength = 0;int i = 0;while(string[i] != "\0"){originalLength++;if(string[i] == " ")countSpace++;i++;}int lengthAfterReplace = originalLength + countSpace * 2;if(lengthAfterReplace > length)return;int indexAfterReplace = lengthAfterReplace;int index = originalLength;while(index >= 0 && indexAfterReplace >= 0){if(string[index] != " ")string[indexAfterReplace--] = string[index--];else{string[indexAfterReplace--] = "0";string[indexAfterReplace--] = "2";string[indexAfterReplace--] = "%";index--;}}
3.JAVA的字符串反转
public class StringReverse{public static void main(String[] args)[String s = "A quick brown fox jumps over the lazy dog.";System.out.println("原始的字符串:" + s);System.out.print("反转后字符串:");// 方法一for(int i = s.length(); i > 0; i--){System.out.print(s.charAt(i - 1));}//方法二char[] data = s.toCharArray();for(int i = data.length; i > 0; i--){System.out.print(data[i - 1]);}//方法三StringBuffer sb = new StringBuffer(s);System.out.print(sb.reverse().toString());}}
to be continued:
JAVA不调用库函数实现字符串反转 回文 替换等等
- C/C++的字符串和JAVA的String对象
- C风格字符串和C++string对象的相互转化
- C风格字符串和C++string对象的相互转化
- C++string对象和字符串字面值的连接,+操作
- string对象和C字符串之间的转换
- String对象和C字符串之间的转换
- C语言 字符串和C++ string对象的相互转化
- Java中JNI的使用详解第四篇:C/C++中创建Java对象和String字符串对象及对字符串的操作方法
- c字符串、string对象、字符串字面值的区别
- c字符串、string对象、字符串字面值的区别
- c字符串、string对象、字符串字面值的区别
- c字符串、string对象、字符串字面值的区别
- C字符串和标准的C++string类
- C字符串和标准的C++string类
- c-风格字符串和string类型的字符串的异同
- string 对象与 C 风格字符串的混用
- C++ string对象和C风格字符串的差别与转换接口
- c++string字符串的输出
- Ubuntu下使用苹果Monaco字体
- hdu 2099 整除的尾数
- loner_li JQuery插件 实现相册光箱特效(后台数据库调图片和前台直接显示)
- HTTP请求流程(二)----Telnet模拟HTTP请求
- ubuntu下firefox安装flash
- C/C++的字符串和JAVA的String对象
- 『免费素材』15套提升设计的高质量纹理素材下载
- 米勒-拉宾素数测试
- apue习题10.5
- POJ 2352 Stars
- 关于今后的学习的思考
- hdu 4217
- UIIImageView 播放图片动画
- linux_取得路径文件名和目录名