Java小记——String类

目录

字符串概念

String类的构造方法

String类的特点

==和equals的区别

String类的判断方法

String类的获取方法

String类的转换方法


字符串概念

1.字符串是由一个或多个字符组成的一串数据(字符序列)
2.字符串可以看成是字符数组

String类的构造方法

1.空参构造

String str = new String();

String 类重写了toString()方法,打印这个字符串的内容

System.out.println(str);

结果:

                          //空字符串

2.参数为字符串 //字符串常量值转成字符串

String s = new String("hello");

System.out.println(s);

结果:

hello

3.参数为字节数组 //把字节数组转成字符串

byte[] bytes = {97, 98, 99, 100};

String s = new String(bytes);

System.out.println(s)

结果:

abcd

把字节数组的一部分转成字符串

byte[] bytes = {97, 98, 99, 100};

String s1 = new String(bytes, 2, 2); //从2索引开始,取后两个转成字符串

System.out.println(s1);

结果:

cd

4.参数为字符数组 //把字符数组转成字符串

char[] chars = {'A', 'B', 'C', '我', '爱', '你'};

String string = new String(chars);

System.out.println(string);

结果:

ABC我爱你

把字符数组的一部分转成字符串

char[] chars = {'A', 'B', 'C', '我', '爱', '你'};

String s = new String(chars, 3, 3); //从3索引处开始,取后三个转成字符串
System.out.println(s);

结果:

我爱你

String类的特点

1.字符串字面值"abc"也可以看成是一个字符串对象。

"abc"                    //为一对象    引用存在字符串常量池中

String s1 = "abc"; //为一对象

2.字符串是常量,一旦被创建,就不能被改变。值不能被改变,你能改变的是引用或者说指向。

String s = "hello";
s = "world" + "java";

System.out.println(s);

结果:

worldjava

 3. 定义一个字符串时,会先在字符串常量池中找有没有这个字符串,若没有,则在堆中创建一个对象,在常量池中存放它的索引,若已存在这个字符串,则无需创建对象,在常量池中找到这个索引即可。

String s = new String("hello"); //创建两个对象

String s = "hello";  //创建一个对象

当两条语句一起出现时,也只创建两个对象,因为字符串常量池中已有hello的索引,无需再创建对象。

==和equals的区别

 ==是一个比较运算符,可以比较基本数据类型,也可以比较引用数据类型

比较基本数据类型,则比较两个变量的值是否相等

比较引用数据类型,则比较两个对象的地址值是否相等

equals() 他是Object类中的一个方法,他只能比较引用数据类型,默认比较的是两个对象的 地址值是否相等。

String类,也重写 equals() 方法,比较的是两个字符串,字面上的内容是否相同。

String类的判断方法

1.equals()     比较字符串的内容是否相同,区分大小写

System.out.println("abc".equals("abc"))

2.equalsIgnoreCase()     比较字符串的内容是否相同,忽略大小写

System.out.println("Abc".equalsIgnoreCase("aBc"))

3.contains()      判断字符串中是否包含传递进来的字符串

System.out.println("abc".contains("ab"))

4.startsWith()     判断字符串是否以传递进来的字符串开头

 System.out.println("abc".startsWith("a"));

5.endsWith      判断字符串是否以传递进来的字符串结尾

System.out.println("abc".endsWith("c"));

6.isEmpty       判断字符串的内容是否为空串""

System.out.println("".isEmpty());

String类的获取方法

1.length ()    获取字符串的长度

int len = "abc".length();

System.out.println(len);

2.charAt(index)      获取指定索引位置的字符

char ch = "abc".charAt(1);
System.out.println(ch);

3.indexOf(ch)       返回指定字符在此字符串中第一次出现处的索引

String s = "abc";
int index = s.indexOf('b');

System.out.println(index);

查不到就返回-1

4.indexOf(str)     返回指定字符串在此字符串中第一次出现处的索引

String s = "abc";
int index = s.indexOf("bc");

System.out.println(index);

查不到就返回-1

5.indexOf (str  index)    返回指定字符串在此字符串中从指定位置后第一次出现处的索引

String s = "返回指定字符回在此字符串中第一次出现处的索引一"

int i = s.indexOf("回", 2);
 System.out.println(i);

6.substring (start)     从指定位置开始截取字符串, 默认到末尾

String str = "从指定位置开始截取字符串, 默认到末尾";

String substring1 = str.substring(5); 
System.out.println(substring1);  //结果:开始截取字符串, 默认到末尾

7.substring ( start, end)   从指定位置开始到指定位置结束截取字符串

String str = "从指定位置开始截取字符串, 默认到末尾";
String substring = str.substring(0, 5); 
System.out.println(substring);  //结果:从指定位置

String类的转换方法

1.getBytes()    把字符串转换为字节数组

byte[] bytes = str.getBytes();
for (byte aByte : bytes) {
     System.out.println(aByte);
}

把字节数组转换为字符串

String s = new String(bytes);
System.out.println(s);

2.toCharArray()    把字符串转换为字符数组

char[] chars = s2.toCharArray()

把字符数组转换成字符串

String s1 = new String(chars);
System.out.println(s1);

3.valueOf(char[] chs/int i)     把任意类型的数据转成字符串

String s = String.valueOf(50);
String s1 = String.valueOf(3.25);

String s3 = String.valueOf(false);

String s4 = String.valueOf(new char[]{'A', 'B', 'C'});

4.toLowerCase()    把字符串转成小写

   toUpperCase()    把字符串转成大写

System.out.println("abc".toUpperCase());
System.out.println("ABC".toLowerCase());

5.concat(str)      字符串拼接

String concat = "你好".concat("世界").concat("爱生活").concat("爱Java");

System.out.println(concat);

6.replace(char old , char new)      将指定字符进行互换

String str = "abcd"

String s = str.replace('b', '*');
System.out.println(s);

7.replace(String old,String new)       将指定字符串进行互换

String str = "abcd"

String s = str.replace("abc", "df");
System.out.println(s);

8.trim()    去除两端空格

String ss = "    呵  呵    ";
String trim = ss.trim();

System.out.println(trim);

9.compareTo(str)   顺序比较两个字符串  相同返回0 前一个多返回1 后一个多返回-1

String s1 = "abc";
String s2 = "abcd";

int i = s1.compareTo(s2);
System.out.println(i);
打印 -1

String s1 = "abc";
String s2 = "abc";

int i = s1.compareTo(s2);
System.out.println(i);

打印0

String s1 = "abcd";
String s2 = "abc";

int i = s1.compareTo(s2);
System.out.println(i);

打印1

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
接入第三方登录是让用户方便快捷地使用已有账号登录你的网站或应用程序,提高用户体验的一种方式。本文将介绍如何使用 PHP 实现微信公众号第三方登录。 1. 获取微信授权 首先,需要获取微信用户的授权。具体步骤如下: 1)引导用户打开微信授权页面: ```php $appid = 'your_appid'; $redirect_uri = urlencode('http://yourdomain.com/callback.php'); $scope = 'snsapi_userinfo'; $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=$scope&state=STATE#wechat_redirect"; header("Location: $url"); ``` 其中,`$appid` 是你的微信公众号的 AppID,`$redirect_uri` 是授权后回调的 URL,`$scope` 是授权作用域,可以是 `snsapi_base` 或 `snsapi_userinfo`,`$state` 是自定义参数,用于防止 CSRF 攻击。 2)获取授权码: 用户同意授权后,会重定向到 `$redirect_uri` 指定的 URL,带上授权码 `code` 和 `state` 参数。 ```php $code = $_GET['code']; $state = $_GET['state']; ``` 3)获取 access_token 和 openid: 使用授权码 `code` 获取 `access_token` 和 `openid`。 ```php $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code"; $response = file_get_contents($access_token_url); $result = json_decode($response, true); $access_token = $result['access_token']; $openid = $result['openid']; ``` 其中,`$secret` 是你的微信公众号的 AppSecret。 2. 获取用户信息 获取到 `access_token` 和 `openid` 后,可以使用以下代码获取用户信息: ```php $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"; $response = file_get_contents($userinfo_url); $userinfo = json_decode($response, true); ``` 其中,`$userinfo` 包含用户的昵称、头像等信息。 3. 将用户信息保存到数据库 最后,将获取到的用户信息保存到数据库中,以便下次使用时快速登录。 ```php // 连接数据库 $con = mysqli_connect('localhost', 'username', 'password', 'database'); mysqli_set_charset($con, "utf8"); // 查询用户是否已存在 $sql = "SELECT * FROM users WHERE openid='$openid'"; $result = mysqli_query($con, $sql); if (mysqli_num_rows($result) == 0) { // 用户不存在,插入新用户信息 $nickname = mysqli_real_escape_string($con, $userinfo['nickname']); $headimgurl = mysqli_real_escape_string($con, $userinfo['headimgurl']); $sql = "INSERT INTO users (openid, nickname, headimgurl) VALUES ('$openid', '$nickname', '$headimgurl')"; mysqli_query($con, $sql); } // 保存用户登录状态 $_SESSION['openid'] = $openid; ``` 以上就是使用 PHP 实现微信公众号第三方登录的步骤。需要注意的是,为了确保安全性,应该对用户输入的数据进行过滤和验证,防止 SQL 注入和 XSS 攻击等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jmh-Ethereal

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值