黑马程序员---String类

---------------------- android培训java培训、期待与您交流! ----------------------


String类是我们经常用到的,它里面的属性和方法能帮我们解决非常多的问题,比如:比较两个字符串,替换字符,查找字符,判断字符串是否含有特定的字符串等等。

这次学习到了一些string类的属性和方法,在这做一下归纳和总结。

 

首先,string定义的变量可以看作是char的只读数组。

比如:

string s ="hello,world!";

           Console.WriteLine(s[1]);

运行结果为:e 即是上面字符串的第2个字母。

 

1. 一些小知识

· 字符串的连接 + ,两边只要有一个字符串类型,另一边也会被自动转换成字符串类型。

· 一切对象都可以调用 .ToString()方法转换成字符串类型。

2. string类中的一些常用的方法:

· ToLower()得到字符串的小写形式并返回。

· ToUpper()得到字符串的大写形式并返回。

注意:字符串是不可变的,所以这些函数都不能直接改变字符串内容,而是把修改后的字符串的值通过函数返回值的形式返回。

· s1.Equals(s2) 用于比较用于s1和s2两个字符串的比较,若相等返回值为true,反之为false。

· s1.Equals(s2,StringComparison.OrdinalIgnoreCase)用于s1和s2两个字符串不区分大小写的比较。

以上两个为方法的重载。

 

·string[] Split(params char[] separtor)  将字符串按照指定的分隔符分割为字符串数组。

·string[] Split(char[] separtor, StringSplitOptions.RemoveEmptyEntries)  将字符串按照指定的分隔符分割为字符串数组,并移除空白字符。

·字符串替换 string Replace(string oldvalue,string new value)通俗的                 实例名. Replace(”老的”,”新的(用于替换老的)”)

·取子字符串:string Substring(int start Index) 取从位置start Index(位置数字)到最后的子字符串。

·string Substring(int start Index,intlength) 取从位置start Index(位置数字)长度为length的子字符串。

·bool Contains(string value)   判断字符串中是否含子串value

·boolStartsWith(string value) 判断字符串是否以子串value开始

·boolEndWith(string value)    判断字符串是否以子串value结束

·int IndexOf(string value)  取子串value第一次出现的位置(没有value则返回 -1)

·int Index Of(string value,int start Index)    取子串value,start Index(一个表示位置的数字)出现的位置(没有value则返回 -1)





---------------------- android培训java培训、期待与您交流! ----------------------
### Spring Security入门案例与黑马程序员教程 Spring Security 是一个强大的安全框架,用于保护基于 Java 的应用程序。它提供了认证、授权和防止常见攻击的功能。以下是一个简单的入门案例,结合了 Spring Boot 和 Spring Security 的基本配置。 #### 1. 引入依赖 在 `pom.xml` 文件中添加 Spring Security 的启动器依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 引入此依赖后,Spring Security 将自动为项目提供基础的安全保护[^2]。 #### 2. 配置安全规则 创建一个配置来定义访问规则。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 允许所有用户访问 /public 路径 .anyRequest().authenticated() // 其他路径需要认证 .and() .formLogin() .loginPage("/login") // 自定义登录页面 .permitAll() .and() .logout() .permitAll(); } } ``` 上述代码定义了访问规则:`/public/**` 路径对所有用户开放,而其他路径需要经过身份验证才能访问[^2]。 #### 3. 创建用户认证信息 可以通过内存中的用户数据进行简单认证。例如: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class AuthConfig { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password(passwordEncoder().encode("password")).roles("USER") .and() .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN"); } } ``` 这里定义了两个用户:`user` 和 `admin`,分别拥有不同的角色权限[^2]。 #### 4. 创建控制器 为了测试安全规则,可以创建一个简单的控制器: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/public/hello") public String publicHello() { return "Hello, this is a public resource!"; } @GetMapping("/private/hello") public String privateHello() { return "Hello, this is a private resource!"; } } ``` 访问 `/public/hello` 不需要认证,而访问 `/private/hello` 则需要认证。 #### 5. 测试应用 运行 Spring Boot 应用程序后,尝试访问以下 URL: - `/public/hello`:可以直接访问。 - `/private/hello`:将被重定向到登录页面,输入用户名和密码后可以访问。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值