百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Java字符串对象详细总结(初学必看)

lipiwang 2024-11-01 14:13 6 浏览 0 评论

《大数据和人工智能交流》头条号向广大初学者新增C 、Java 、Python 、Scala、javascript 等目前流行的计算机、大数据编程语言,希望大家以后关注本头条号更多的内容。


1、创建字符串

(1)方式1

String str1=new String("abc");

String str2=new String("abc");

System.out.println(str1);


(2)方式2

char c[]={'a','b','c'};

String s=new String(c);

System.out.println(s);


2、通过调用length()方法得到String的长度.


public static void main(String[] args)

{

String s="abc";

int len=s.length();

System.out.println(len);

}


3、charAt()方法:去字符串中的字符

String s="abc";

char c=s.charAt(1);

System.out.println(c);


面试题:有个字符串s="abc";请讲字符串翻转ss="cba";

private static String reverseStr(String s) {

String ss="";

for(int i=s.length()-1;i>=0;i--)

{

ss+=s.charAt(i);

}

return ss;

}

4、String的split方法

String s="s001,lily,21,F,100";

String[] str = s.split(",");

for(String ss:str)

{

System.out.println(ss);

}

示例:

public class Demo {


public String show()

{

int count=0;

String ss="";

for(int i=1;i<=100;i++)

{

count=0;

for(int j=1;j<=i;j++)

{

if(i%j==0)

{

count++;

}

}

if(count==2)

{

ss=ss+i+";";

}

}

return ss;

}

public static void main(String[] args)

{

Demo d=new Demo();

String s = d.show();

String[] str = s.split(";");

if(!"".equals(str))

{

for(String s1:str)

{

System.out.print(s1+" ");

}

}

}


}

5、indexOf方法

indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring


示例:

public static void main(String[] args)

{

String s="abcded";

int index = s.indexOf('d');

System.out.println(index);


}

6、subString()是提取字符串的另一种方法,它可以指定从何处开始提取字符串以及何处结束。


【1】substring(int beginIndex)

Returns a new string that is a substring of this string.

示例:

public static void main(String[] args)

{

String s="abcded";

String ss = s.substring(2);

System.out.println(ss);


}

【2】String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string.


示例:

public static void main(String[] args)

{

String s="abcdedety";

String ss = s.substring(2,5);

System.out.println(ss);


}

7、剪空格trim

public static void main(String[] args)

{

String s=" abc ";

System.out.println("剪掉前:====="+s+"====");

String ss = s.trim();

System.out.println("剪掉后:====="+ss+"====");

}


8、字符串替换函数


【1】replace(char oldChar, char newChar)

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.


【2】replace(CharSequence target, CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.


【3】String replaceAll(String regex, String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement.


Parameters:

regex: the regular expression to which this string is to be matched

replacement :the string to be substituted for each match


示例1:使用空串来替换空格

public static void main(String[] args)

{

String s="abc abc abc";

String ss=s.replaceAll(" ", "");

System.out.println(ss);

}

示例2:

public static void main(String[] args)

{

String s="abc我靠abc我靠abc";

String ss=s.replaceAll("我靠", "");

System.out.println(ss);

}


9、字符串链接

public static void main(String[] args)

{

String s="aaa";

s=s.concat("111").concat("222");

System.out.println(s);


}


10、修改可变字符串StringBuffer


(1)StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在

进行字符串处理时,不生成新的对象,在内存使用上要优于String类。所以在实际使用时,如果经常需要对一个字符串进行修改,

例如插入、删除等操作,使用StringBuffer要更加适合一些。


(2))在StringBuffer类中存在很多和String类一样的方法,这些方法在功能上和String类中的功能是完全一样的。


(3)但是有一个最显著的区别在于,对于StringBuffer对象的每次修改都会改变对象自身,这点是和String类最大的区别。


另外由于StringBuffer是线程安全的,关于线程的概念后续有专门的章节进行介绍,所以在多线程程序中也可以很方便的进行使用,

但是程序的执行效率相对来说就要稍微慢一些。


StringBuffer类为可变字符串的修改提供了3种方法,在字符串中间插入和改变某个位置所在的字符。

【1】在字符串后面追加:用append()方法将各种对象加入到字符串中。

【2】在字符串中间插入:用insert()方法。例


StringBuffer str=new StringBuffer("This is a String");

Str.insert(9,"test");

System.out.println(str.toString());

示例1:

public static void main(String[] args)

{

StringBuffer sf=new StringBuffer("hello");

sf.append(100);

sf.append("me");

System.out.println(sf);

}

示例2:

public static void main(String[] args)

{

String s="abc";

StringBuffer ss=new StringBuffer();

for(int i=s.length()-1;i>=0;i--)

{

ss.append(s.charAt(i));

}

System.out.println(ss.toString());

}


相关推荐

ubuntu单机安装open-falcon极度详细操作

备注:以下操作均由本人实际操作并得到验证,喜欢的同学可尝试操作安装。步骤一1.1环境准备(使用系统:ubuntu18.04)1.1.1安装redisubuntu下安装(参考借鉴:https://...

Linux搭建promtail、loki、grafana轻量日志监控系统

一:简介日志监控告警系统,较为主流的是ELK(Elasticsearch、Logstash和Kibana核心套件构成),虽然优点是功能丰富,允许复杂的操作。但是,这些方案往往规模复杂,资源占用高,...

一文搞懂,WAF阻止恶意攻击的8种方法

WAF(Web应用程序防火墙)是应用程序和互联网流量之间的第一道防线,它监视和过滤Internet流量以阻止不良流量和恶意请求,WAF是确保Web服务的可用性和完整性的重要安全解决方案。它...

14配置appvolume(ios14.6配置文件)

使用AppVolumes应用程序功能,您可以管理应用程序的整个生命周期,包括打包、更新和停用应用程序。您还可以自定义应用程序分配,以向最终用户提供应用程序的特定版本14.1安装appvolume...

目前流行的缺陷管理工具(缺陷管理方式存在的优缺点)

摘自:https://blog.csdn.net/jasonteststudy/article/details/7090127?utm_medium=distribute.pc_relevant.no...

开源数字货币交易所开发学习笔记(2)——SpringCloud

前言码云(Gitee)上开源数字货币交易所源码CoinExchange的整体架构用了SpringCloud,对于经验丰富的Java程序员来说,可能很简单,但是对于我这种入门级程序员,还是有学习的必要的...

开发JAX-RPC Web Services for WebSphere(下)

在开发JAX-RPCWebServicesforWebSphere(上)一文中,小编为大家介绍了如何创建一个Web服务项目、如何创建一个服务类和Web服务,以及部署项目等内容。接下来小编将为大...

CXF学习笔记1(cxf client)

webservice是发布服务的简单并实用的一种技术了,个人学习了CXF这个框架,也比较简单,发布了一些笔记,希望对笔友收藏并有些作用哦1.什么是webServicewebService让一个程序可...

分布式RPC最全详解(图文全面总结)

分布式通信RPC是非常重要的分布式系统组件,大厂经常考察的Dubbo等RPC框架,下面我就全面来详解分布式通信RPC@mikechen本篇已收于mikechen原创超30万字《阿里架构师进阶专题合集》...

Oracle WebLogic远程命令执行0day漏洞(CVE-2019-2725补丁绕过)预警

概述近日,奇安信天眼与安服团队通过数据监控发现,野外出现OracleWebLogic远程命令执行漏洞最新利用代码,此攻击利用绕过了厂商今年4月底所发布的最新安全补丁(CVE-2019-2725)。由...

Spring IoC Container 原理解析(spring中ioc三种实现原理)

IoC、DI基础概念关于IoC和DI大家都不陌生,我们直接上martinfowler的原文,里面已经有DI的例子和spring的使用示例《InversionofControlContainer...

Arthas线上服务器问题排查(arthas部署)

1Arthas(阿尔萨斯)能为你做什么?这个类从哪个jar包加载的?为什么会报各种类相关的Exception?我改的代码为什么没有执行到?难道是我没commit?分支搞错了?遇到问题无法在...

工具篇之IDEA功能插件HTTP_CLENT(idea2021插件)

工具描述:Java开发人员通用的开发者工具IDEA集成了HTTPClient功能,之后可以无需单独安装使用PostMan用来模拟http请求。创建方式:1)简易模式Tools->HTTPCl...

RPC、Web Service等几种远程监控通信方式对比

几种远程监控通信方式的介绍一.RPCRPC使用C/S方式,采用http协议,发送请求到服务器,等待服务器返回结果。这个请求包括一个参数集和一个文本集,通常形成“classname.meth...

《github精选系列》——SpringBoot 全家桶

1简单总结1SpringBoot全家桶简介2项目简介3子项目列表4环境5运行6后续计划7问题反馈gitee地址:https://gitee.com/yidao620/springbo...

取消回复欢迎 发表评论: