创建字符串

  • 创建字符串对象-1
public class Test {public static void main(String[] args) {String s = new String(); //创建字符串System.out.println(s);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 创建字符串对象-2
public class Test {public static void main(String[] args) {char a[] = {'g', 'o', 'o', 'd'};String s = new String(a); // 创建字符串System.out.println(a);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 创建字符串对象-3
public class Test {public static void main(String[] args) {char[] a = {'s','t','u','d','e','n','t'};String s = new String(a, 2, 4);System.out.println(s);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

字符串操作

  • 字符串操作-拼接(+)
public class Test {public static void main(String[] args) {String s1 = new String("Hello");String s2 = new String("world");String s = s1 + " " + s2;System.out.println(s);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 字符串操作-拼接(+)
public class Test {public static void main(String[] args) {int booktime = 4;float practice = 2.5f;System.out.println("我每天花费" + booktime + "小时看书;" + practice + "小时上机练习");}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 字符串操作-获取字符串长度
public class Test {public static void main(String[] args) {String s = "We are students";System.out.println("字符串的长度是:" + s.length());}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 字符串操作 - 获取指定字符的索引位置
public class Test {public static void main(String[] args) {String s = "We are students";System.out.println("字符s在字符串s中的位置是:" + s.indexOf("s"));System.out.println("字符st在字符串s中的位置是:" + s.indexOf("st"));}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
public class Test {public static void main(String[] args) {String s = "We are students";System.out.print("字符s在字符串s中的最后位置是:");System.out.println(s.lastIndexOf("s"));}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 字符串操作-获取指定索引位置的字符
public class Test {public static void main(String[] args) {String s = "hello world";char mychar2 = s.charAt(6);System.out.println("字符串s中索引位置是6的字符位:" + mychar2);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 字符串操作-去除前导和尾部空格
public class Test {public static void main(String[] args) {String s1 = "   Java class    ";String s2 = s1.trim();System.out.println("字符串原来的长度:" + s1.length());System.out.println("去除空格后的长度:" + s2.length());System.out.println("去除空格后的字符串是:" + s2);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 字符串操作-去除字符串中的所有空格
import java.util.StringTokenizer;
public class Test {public static void main(String[] args) {String text = "  We are student  ";System.out.println("原来字符串是:");System.out.println(text);StringTokenizer st = new StringTokenizer(text, " ");StringBuffer sb = new StringBuffer();int i = 1;while (st.hasMoreElements()) {i++;sb.append(st.nextToken());}System.out.println("去掉字符串中所有空格之后的字符串是:");System.out.println(sb.toString());}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
public class Test {public static void main(String[] args) {String s = "J a v a 编 程 词  典  ";s = s.replaceAll(" ", "");System.out.println("去掉空格后的字符串为:" + s);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 字符串操作-字符串替换
public class Test {public static void main(String[] args) {String s = "bad bad study";String news = s.replace("bad", "good");System.out.println("替换后的字符串是:" + news);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
public class Test {public static void main(String[] args) {String s = "bad bad study";s = s.replaceFirst("bad", "good");System.out.println("替换后的字符串是:" + s);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 字符串操作-判断字符串是否相等
public class Test {public static void main(String[] args) {String s1 = new String("I am a student");String s2 = new String("I am a student");String s3 = new String("I AM A STUDENT");String s4 = s1;boolean b1 = (s1 == s2);boolean b2 = (s1 == s4);boolean b3 = s1.equals(s2);boolean b4 = s1.equals(s3);boolean b5 = s1.equalsIgnoreCase(s2);boolean b6 = s1.equalsIgnoreCase(s3);System.out.println("s1 == s2:" + b1);System.out.println("s1 == s4:" + b2);System.out.println("s1 equals s2:" + b3);System.out.println("s1 equals s3:" + b4);System.out.println("s1 equalsIgnoreCase s2:" + b5);System.out.println("s1 equalsIgnoreCase s3:" + b6);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 字符串操作-判断字符串的开始与结尾
public class Test {public static void main(String[] args) {String num1 = "22045612";String num2 = "21304578";boolean b1 = num1.startsWith("22");boolean b2 = num1.endsWith("78");boolean b3 = num2.startsWith("22");boolean b4 = num2.endsWith("78");System.out.println("字符串num1是以'22'开始的吗?" + b1);System.out.println("字符串num1是以'78'结束的吗?" + b2);System.out.println("字符串num2是以'22'开始的吗?" + b3);System.out.println("字符串num2是以'78'结束的吗?" + b4);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 字符串操作-字母大小写转换
public class Test {public static void main(String[] args) {String s = new String("abc DEF");String news1 = s.toLowerCase();String news2 = s.toUpperCase();System.out.println("原字符串:" + s);System.out.println("全部转换成小写字母后的字符串:" + news1);System.out.println("全部转换成大写字母后的字符串:" + news2);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 字符串操作-字符串分割
public class Test {public static void main(String[] args) {String s = new String("abc,def,ghi,gkl");String[] news = s.split(",");System.out.println("原字符串:" + s);System.out.println("按分割字符分割后的字符串是:");for (int i=0; i<news.length; i++) {System.out.println(news[i]);}}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
public class Test {public static void main(String[] args) {String s = new String("abc,def,ghi,gkl");String[] news2 = s.split(",", 2);for (int j=0; j<news2.length; j++) {System.out.println("按分割字符分割一次后的字符串是:");System.out.println(news2[j]);}}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 实例 - 根据指定分隔符把字符串分行
public class Test {public static void main(String[] args) {String s = "无言独上西楼,月如钩,寂寞梧桐深院锁清秋。" + "剪不断、理还乱、是离愁,别是一般滋味在心头。";System.out.println("源字符串:" + s);System.out.println("源字符串的长度是:" + s.length());String[] news = s.split(",|。");for (int i=0; i<news.length; i++) {System.out.println(news[i]);}System.out.println("分行后字符串数组的长度是:" + news.length);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 实例 - 判断字符串是否是数字格式
public class Test {public static void main(String[] args) {String s = "12312312";if (IsNumber(s)) {System.out.println(s + "是数字格式");} else {System.out.println(s + "不是数字格式");}}public static boolean IsNumber(String str) {char[] c = str.toCharArray();for (int i=0; i<c.length; i++) {if (Character.isDigit(c[i]));elsereturn false;}return true;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

随笔

  • Eclipse

    • Babel可以汉化Eclipse
    • Ctrl+Alt+/:自动补全Java关键字
    • Alt+/:启动Eclipse代码辅助菜单
from: http://blog.csdn.net/xuezhisdc/article/details/52185261

编码练习——Java-4-字符串相关推荐

  1. java测试字符串的编码_Java字符串测验

    java测试字符串的编码 Welcome to Java String Quiz. String is one of the most important classes in Java. If yo ...

  2. Java 中文字符串编码之GBK转UTF-8

    一.乱码的原因 gbk的中文编码是一个汉字用[2]个字节表示,例如汉字"内部"的gbk编码16进制的显示为c4 da b2 bf utf-8的中文编码是一个汉字用[3]个字节表示, ...

  3. jni和java之间字符串的转换

    jni和java之间字符串的转换方法. C的实现: JNIEXPORT jstring JNICALL Java_Android123_CwjC (JNIEnv *env, jobject obj, ...

  4. java操作字符串的工具类StringUtil

    依赖引入 <dependency><groupId>commons-lang</groupId><artifactId>commons-lang< ...

  5. java 字符串 加密_如何用JAVA实现字符串简单加密解密?

    展开全部 java加密字符串可以使用des加密算法62616964757a686964616fe4b893e5b19e31333363376462,实例如下: package test; import ...

  6. 【JAVA编码】 JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换

    http://blog.csdn.net/qinysong/article/details/1179489 这两天抽时间又总结/整理了一下各种编码的实际编码方式,和在Java应用中的使用情况,在这里记 ...

  7. java中字符串的精确匹配_Java最佳实践–字符串性能和精确字符串匹配

    java中字符串的精确匹配 在使用Java编程语言时,我们将继续讨论与建议的实践有关的系列文章,我们将讨论String性能调优. 我们将专注于如何有效地处理字符串创建, 字符串更改和字符串匹配操作. ...

  8. c++中string插入一个字符_Java内存管理-探索Java中字符串String(十二)

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 一.初识String类 首先JDK API的介绍: public final class String extends O ...

  9. Java中字符串的学习(一)String类的概述及常见方法使用

    转载请注明出处http://www.cnblogs.com/devtrees/p/4347079.html (拓展:Api:编程语言对外给我们提供的应用程序接口.) 一.概述: 我们平时上网发帖,帖子 ...

  10. Java字符字符串类

    Java字符字符串类 Character 类 Character 类用于对单个字符进行操作.Character 类在对象中包装一个基本类型 char 的值在实际开发过程中,我们经常会遇到需要使用对象, ...

最新文章

  1. springboot整个cas_SpringBoot集成SpringSecurity+CAS
  2. 锐捷路由器--多线路应用路由
  3. Win32中GDI+应用(三)---Graphics类
  4. 用分类行为解释为什么破碎的鸡蛋不能还原为一个完整的鸡蛋
  5. ST17H26对接RC522读IC卡
  6. Jenkins+ant+Jenkins接口持续集成测试配置
  7. matlab_simulink笔记01——模块属性的设置以及模块参数的设置
  8. Win8消费者预览版下载地址 包含中文下载地址及中文手册
  9. Java静态类使用 使用 service
  10. c2c旅游springboot开源_重量级开源的商城和SpringBoot等项目看看有没有正好是你需要的...
  11. java集合笔试编程题_Java 基础算法及编程笔试题集合
  12. C++基础知识总结----类的进阶知识点
  13. Android 保持屏幕常亮
  14. 计算机msvcp100.dll,msvcp100.dll丢失怎样修复
  15. 【CS学习笔记】14、powerup提权的方法
  16. Cognos资料整理
  17. android音视频通话解决方案,Android 音视频通话通知说明
  18. c编程语言real,20 种最奇怪的编程语言
  19. 服务器2012分辨率不能修改,《F1 2012》无法修改分辨率解决方法
  20. anmate.css怎么用,animate.css使用方法是什么

热门文章

  1. Linux shell的和||--转载
  2. Lesson 15.2 学习率调度在PyTorch中的实现方法
  3. Lesson 14.3 Batch Normalization综合调参实战
  4. 【自然语言处理】自然语言处理(NLP)知识结构总结
  5. Google发布新API,支持训练更小、更快的AI模型
  6. 百度计算广告学沙龙学习笔记 - 内容匹配广告
  7. 布道微服务_07服务调用追踪
  8. jvm性能调优 - 21案例实战_百万级用户的在线系统如何基于G1垃圾回收器优化性能
  9. Spring Cloud【Finchley】-17 使用Zuul为单个或全部微服务提供容错与回退功能
  10. MyBatis-17MyBatis代码生成器(逆向工程)MBG使用