java基础第十一天:Object+String

Object

1.概述

是类层次结构的根类。每个类都使用 Object 作为超类。

2.创建对象

Object() —无参构造

3.常用方法

StringtoString() 返回该对象的字符串表示。
booleanequals(Object obj) 指示其他某个对象是否与此对象“相等”。
inthashCode() 返回该对象的哈希码值。

String

1.概述

所有被一对双引号引起来的数据都是String类型

String 类代表字符串。字符串是常量;它们的值在创建之后不能更改。

String本质上是维护一个char[]来存储多个字符数据

2.源码

public final class String

private final char value[];常量,值不能被修改

3.创建对象

`**String**(char[] value)`   

4.常用方法

String s=“helloworld”; System.out.println();

char**charAt**(int index) 返回指定索引处的 char 值。
System.out.println(s.charAt(2)); —‘l’
String**concat**(String str) 将指定字符串连接到此字符串的结尾。
System.out.println(s.concat(“123”));—“hello123”
boolean**contains**(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。
System.out.println(s.contains(“xyz”));—false
boolean**endsWith**(String suffix) 测试此字符串是否以指定的后缀结束。
System.out.println(s.endWith(“o”));—true
boolean**equals**(Object anObject) 将此字符串与指定的对象比较。
System.out.println(s.equals(“llo”));//false
int**hashCode**() 返回此字符串的哈希码。
System.out.println(s.hashCode());//-1524582912
int**indexOf**(String str) 返回指定子字符串在此字符串中第一次出现处的索引。
System.out.println(s.indexOf(“wor”));//5
boolean**isEmpty**() 当且仅当 length()0 时返回 true
System.out.println(s.isEmpty());//false
int**lastIndexOf**(String str) 返回指定子字符串在此字符串中最右边出现处的索引。
System.out.println(s.lastIndexOf(“o”));//6
int**length**() 返回此字符串的长度。
System.out.println(s.length());//10
String**replace**(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
System.out.println(s.replace(‘l’, ‘c’));//heccoworcd
boolean**startsWith**(String prefix, int toffset) 测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
System.out.println(s.startsWith(“e”));//false
String**substring**(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
System.out.println(s.substring(1));//elloworld
String**substring**(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。含头不含尾
System.out.println(s.substring(1, 3));//el
String**toLowerCase**() 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
System.out.println(s.toLowerCase());//helloworld
String**toUpperCase**() 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
System.out.println(s.toUpperCase());//HELLOWORLD
String**trim**() 返回字符串的副本,忽略前导空白和尾部空白。
System.out.println(s.trim());//helloworld //去除前面和后面的多余空格
static String**valueOf**(char c) 返回 char 参数的字符串表示形式。
System.out.println(String.valueOf(‘l’));//e 把各种类型转成String类型
byte[]**getBytes**() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
byte[] a = s.getBytes();
System.out.println(Arrays.toString(a));//[104, 101, 108, 108, 111, 119, 111, 114, 108, 100]
char[]**toCharArray**() 将此字符串转换为一个新的字符数组。
char[] b = s.toCharArray();
System.out.println(Arrays.toString(b));//[h, e, l, l, o, w, o, r, l, d]
String[]**split**(String regex) 根据给定正则表达式的匹配拆分此字符串。
String[] c = s.split(“o”);
System.out.println(Arrays.toString©);//[hell, w, rld]

5.练习题

接受用户输入的字符串,并打印每个字符

String sc=new String(System.in).nextLine();

for(int i=0;i<sc.length();i++){

char x = sc.charAt(i);

System.out.println(x);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用c#2017窗体写一个学生基本信息注册基本控件如下1. private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e):当用户点击“用户协议”链接时,触发这个事件处理程序。 2. form1_Load(object sender, EventArgs e):窗体载时调用这个方法。 3. comboBox1.SelectedIndex = 0;:将下拉框选中第一个选项。 4. button2.Enabled = checkBox1.Checked;:根据复选框状态启用或禁用按钮2。 5. info += "账号:" + textBox1.Text.Trim() + "\n";:将用户名拼接到消息框中。 6. info += "密码:" + textBox4.Text.Trim() + "\n";:将密码拼接到消息框中。 7. info += "性别:" + this.GetSexInput() + "\n";:将性别拼接到消息框中。 8. info += "生日:" + dateTimePicker1.Value.ToShortDateString() + "\n";:将生日拼接到消息框中。 9. info += "QQ:" + textBox1.Text.Trim() + "\n";:将QQ号拼接到消息框中。 10. info += "喜爱的编程:" + GetFaavorLangInput() + "\n";:将喜欢的编程语言拼接到消息框中。 11. info += "爱好:" + GetHobbiesInput() + "\n";:将爱好拼接到消息框中。 12. MessageBox.Show(info, "确认你的注册信息!");:弹出一个消息框显示所有输入的信息。 13. bool ValidateInput():定义了一个名为ValidateInput的方法用于验证输入的用户名、密码、性别、生日、QQ和喜欢的编程语言等信息是否正确。 14. string GetSexInput():定义了一个名为GetSexInput的方法用于获取用户的性别。 15. string GetFaavorLangInput():定义了一个名为GetFaavorLangInput的方法用于获取用户的喜欢的编程语言。 16. string GetHobbiesInput():定义了一个名为GetHobbiesInput的方法用于获取用户的爱好
最新发布
06-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值