首页 > 编程语言 >Java-String的常用方法总结

Java-String的常用方法总结

时间:2023-04-03 11:35:02浏览次数:45  
标签:总结 Java String System new 返回值 retVal public

一、String类

  String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能继承。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。

二、String类构造方法

  1、public String()

  无参构造方法,用来创建空字符串的String对象。

  String str1=new String();

  String str2=new String("asdf");

  2、public String(String value)

  String str2=new String("asdf");

  3、public String(char[]value)

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

  String str4=new String(value);

  4、public String(char chars[],int startIndex,int numChars)

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

  String str5=new String(value,1,2);

  5、public String(byte[]values)

  byte[]strb=new byte[]{65,66};

  String str6=new String(strb);

三、String类常用方法

  1、public char charAt(int index)

  参数

  index--字符的索引。

  返回值

  返回指定索引处的字符。

  实例

public class Test{
  public static void main(String args[]){
  String s="www";
  char result=s.charAt(1);
  System.out.println(result);
  }
  }

  以上程序执行结果为:

  w

  2、public boolean equals(Object anObject)

  参数

  anObject--与字符串进行比较的对象。

  返回值

  如果给定对象与字符串相等,则返回true;否则返回false。

  实例

public class Test{
  public static void main(String args[]){
  String Str1=new String("run");
  String Str2=Str1;
  String Str3=new String("run");
  boolean retVal;
  retVal=Str1.equals(Str2);
  System.out.println("返回值="+retVal);
  retVal=Str1.equals(Str3);
  System.out.println("返回值="+retVal);
  }
  }

  以上程序执行结果为:

  返回值=true

  返回值=true

  3、public boolean endsWith(String suffix)

  endsWith()方法用于测试字符串是否以指定的后缀结束。

  参数

  suffix--指定的后缀。

  返回值

  如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回true;否则返回false。注意,如果参数是空字符串,或者等于此String对象(用equals(Object)方法确定),则结果为true。

  实例

public class Test{
  public static void main(String args[]){
  String Str=new String("runooo");
  boolean retVal;
  retVal=Str.endsWith("run");
  System.out.println("返回值="+retVal);
  retVal=Str.endsWith("ooo");
  System.out.println("返回值="+retVal);
  }
  }

  以上程序执行结果为:

  返回值=false

  返回值=true

  4、public boolean equalsIgnoreCase(String anotherString)

  equalsIgnoreCase()方法用于将字符串与指定的对象比较,不考虑大小写。

  参数

  anObject--与字符串进行比较的对象。

  返回值

  如果给定对象与字符串相等,则返回true;否则返回false。

public class Test{
  public static void main(String args[]){
  String Str1=new String("run");
  String Str2=Str1;
  String Str3=new String("run");
  String Str4=new String("RUN");
  boolean retVal;
  retVal=Str1.equals(Str2);
  System.out.println("返回值="+retVal);
  retVal=Str3.equals(Str4);
  System.out.println("返回值="+retVal);
  retVal=Str1.equalsIgnoreCase(Str4);
  System.out.println("返回值="+retVal);
  }
  }

  以上程序执行结果为:

  返回值=true

  返回值=false

  返回值=true

  5、public String replace(char oldChar,char newChar)

  replace()方法通过用newChar字符替换字符串中出现的所有oldChar字符,并返回替换后的新字符串。

  参数

  oldChar--原字符。

  newChar--新字符。

  返回值

  替换后生成的新字符串。

public class Test{
  public static void main(String args[]){
  String Str=new String("hello");
  System.out.print("返回值:");
  System.out.println(Str.replace('o','T'));
  System.out.print("返回值:");
  System.out.println(Str.replace('l','D'));
  }
  }

  以上程序执行结果为:

  返回值:hellT

  返回值:heDDo

  6、public String toLowerCase()

  toLowerCase()方法将字符串转换为小写。

  参数

  无

  返回值

  转换为小写的字符串。

public class Test{
  public static void main(String args[]){
  String Str=new String("WWW");
  System.out.print("返回值:");
  System.out.println(Str.toLowerCase());
  }
  }

  以上程序执行结果为:

  返回值:www

标签:总结,Java,String,System,new,返回值,retVal,public
From: https://blog.51cto.com/u_15739596/6165867

相关文章

  • 【妙用WebView】鸿蒙元服务中如何使用Java Script的API创建地图
    【关键字】webview地图高德腾讯地图百度地图 【问题背景】开发元服务过程中需要用到地图能力:卡片中显示我的快递位置和我的位置信息;PageAbility中可以打开自定义地图,查询POI点,做路径规划、路径推荐等;查看了高德、百度、华为、腾信地图的后发现,各大厂商对鸿蒙系统的支持能......
  • java不等于 等于
    起因:两个一样得String串对比 !=时返回false。 1、如 Stringstr="";Stringstr2=""; 则 str==str2 str!=str2  均返回结果不正确正确用法  str.Equals(str2) 或!str.Equals(str2); 2、在String使用前要做为null判断。写法:!"".Equals(str)&&str......
  • 重学Java设计模式-结构型模式-代理模式
    重学Java设计模式-结构型模式-代理模式内容摘自:https://bugstack.cn/md/develop/design-pattern/2020-06-16-重学Java设计模式《实战代理模式》.html#重学-java-设计模式-实战代理模式「模拟mybatis-spring中定义dao接口-使用代理类方式操作数据库原理实现场景」代理模式介绍......
  • Vue3【Axios网络请求(GET、POST 、并发请求、全局配置 )】(八)-全面详解(学习总结---从入
    ......
  • Vue3【Transition(效果、CSS 过渡、使用animation、TransitionGroup、 KeepAlive、Tele
    ......
  • 校内天梯赛总结
    1107:ZN的随机数#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;intans;intmain(){lln,m;while(cin>>n)//在while中赋值{ans=0;boola[1001]={0};for(inti=0;i<n;i++){intx;cin&g......
  • java 启动错误idea 不报错误
    penapi.actionSystem.impl.Utils-updatefailedforAnAction(com.intellij.execution.ExecutorRegistryImpl$ExecutorAction)withID=MyBatisLogPluginjava.lang.IllegalStateException:@NotNul在项目拉取启动的时候就报出了这个错误首先查看idea的日志到底是什么报错 ......
  • 【组会】water_on_floor_image & 总结
    实验场景channel1channel4文章内容用到的特征机器学习算法OnTheFeasibilityofEstimatingSolubleSugarContentUsingMillimeter-wave60GHz毫米波信号,估计水果中可溶性糖含量(SSC)的可行性RSS,表面粗糙程度,最大幅度值,峰值之间的时间,频域中的通道功率LR,RF......
  • 开源项目总结(产品)
      总结下工作中拿来就能上线使用的一些开源项目,他们能够很好的满足我们的需求,无需从0到1进行开发,快速部署上线,同时可根据实际业务进行二次开发 [电商系统]1.Magento2介绍: 世界排名第一的开源电商系统开发语言:PHP项目地址: magento/magento2中文站: https://www.mall......
  • Java 序列化详解
    XML和JSON是两种经常在网络使用的数据表示格式,这里我们介绍如何使用Java读写XML和JSON。 一、XML概述1、XML简介我们都知道对象是不能在网络中直接传输的,不过还有补救的办法。XML(ExtensibleMarkupLanguage)可扩展标记语言,本身就被设计用来存储数据,任何一个对象都可以用XML来描......