0
点赞
收藏
分享

微信扫一扫

苍穹外卖Day03中不懂的地方记录——SpringBoot补天笔记

雪域迷影 23小时前 阅读 0

哈喽小伙伴们,大家好!今天我们学习的内容是字符串的高级操作

一、字符串高级

字符串的常见操作包括:

获取长度:lenlen函数可以获取字符串的长度。
len函数可以获取字符串的长度。查找指定内容在字符串中是否存在,如果存在就返回该内容在字符串中第一次
出现的开始位置索引值,如果不存在,则返回-1.
判断:startswith,endswith判断字符串是不是以谁谁谁开头/结尾
计算出现次数:count返回 str在start和end之间 在 mystr里面出现的次数
替换内容:replace替换字符串中指定的内容,如果指定次数count,则替换不会超过count次。
切割字符串:split通过参数的内容切割字符串
修改大小写:upper,lower将字符串中的大小写互换
空格处理:strip去空格
字符串拼接:join字符串拼接

代码演示:

1.获取长度: 使用**len**函数可以获取字符串的长度。

s = "hello"
length = len(s)
print(length)  # 输出 5

2.查找内容: 使用**find**方法可以查找指定内容在字符串中第一次出现的位置索引值。

s = "hello"
index = s.find("e")
print(index)  # 输出 1

3.循环遍历字符串: 可以使用**for**循环遍历字符串中的每个字符。

for char in "hello":
    print(char)

4.判断开头和结尾: 使用**startswithendswith**方法判断字符串是否以指定内容开头或结尾。

s = "hello"
print(s.startswith("h"))  # 输出 True
print(s.endswith("o"))    # 输出 True

5.计算出现次数: 使用**count**方法返回指定内容在字符串中出现的次数。

s = "hello,hello,world"
count = s.count("hello")
print(count)  # 输出 2

6.替换内容: 使用**replace**方法替换字符串中的指定内容。

s = "hello,world"
new_s = s.replace("world", "python")
print(new_s)  # 输出 hello,python

7.切割字符串: 使用**split**方法通过指定内容切割字符串,并返回切割后的子串列表。

s = "hello,world"
parts = s.split(",")
print(parts)  # 输出 ['hello', 'world']

8.修改大小写: 使用**upperlower**方法将字符串中的字母转换为大写或小写。

s = "Hello,World"
lowercase = s.lower()
uppercase = s.upper()
print(lowercase)  # 输出 hello,world
print(uppercase)  # 输出 HELLO,WORLD

9.空格处理: 使用**strip**方法去除字符串两端的空格。

s = "  hello,world  "
stripped = s.strip()
print(stripped)  # 输出 hello,world

10.字符串拼接: 使用**join**方法将多个字符串拼接成一个字符串。

parts = ["hello", "world"]
s = ",".join(parts)
print(s)  # 输出 hello,world

二、正则表达式

在Python中,正则表达式是处理字符串的强大工具,可以用来搜索、匹配、替换字符串。下面是一些常见的正则表达式操作:

  1. re模块的使用:Python中使用re模块来操作正则表达式,可以进行搜索、匹配和替换等操作。
import re

# 使用re.match()方法匹配字符串
pattern = r"hello"
string = "hello world"
match = re.match(pattern, string)
if match:
    print("Match found: ", match.group())
else:
    print("No match")

# 使用re.search()方法搜索字符串
search = re.search(pattern, string)
if search:
    print("Search found: ", search.group())
else:
    print("No search")

# 使用re.findall()方法查找所有匹配的字符串
find_all = re.findall(pattern, string)
print("Find all: ", find_all)

# 使用re.sub()方法替换字符串中的匹配项
replace = re.sub(pattern, "hi", string)
print("Replace: ", replace)

  1. 常用的匹配符号
    • .:匹配任意字符
    • ^:匹配字符串的开头
    • $:匹配字符串的结尾
    • ``:匹配前面的字符零次或多次
    • +:匹配前面的字符一次或多次
    • ?:匹配前面的字符零次或一次
    • {}:匹配前面的字符指定次数
    • []:匹配字符集合中的任意一个字符
    • |:或操作,匹配多个表达式中的一个
  2. 示例
import re

# 匹配所有以a开头的单词
pattern = r"\\ba\\w+"
string = "apple banana orange"
matches = re.findall(pattern, string)
print(matches)

# 替换所有数字为"x"
pattern = r"\\d+"
string = "12 apples and 35 bananas"
replace = re.sub(pattern, "x", string)
print(replace)

# 检查字符串是否以数字开头
pattern = r"^\\d+"
string = "123abc"
if re.match(pattern, string):
    print("String starts with a number")
else:
    print("String does not start with a number")

(1)正则表达式**pattern = r"\\ba\\w+"**的含义是匹配以字母"a"开头,后面跟着任意数量的字母或数字的单词。让我们详细解释一下它的构成部分:

  • \\b:表示单词的边界,确保匹配的是单词的开头。
  • a:匹配字母"a"。
  • \\w+:匹配一个或多个字母、数字或下划线。

(2)这个正则表达式**pattern = r"\\d+"**的含义是匹配一个或多个数字。让我们详细解释一下它的构成部分:

  • \\d:匹配任意一个数字字符,相当于**[0-9]**。
  • +:匹配前面的字符(这里是**\\d**)一次或多次。

因此,这个正则表达式可以匹配任意长度的数字串,比如"123"、"4567"等。

(3)

这个正则表达式**pattern = r"^\\d+"**的含义是匹配以一个或多个数字开头的字符串。让我们详细解释一下它的构成部分:

  • ^:表示匹配字符串的开头。
  • \\d:匹配任意一个数字字符,相当于**[0-9]**。
  • +:匹配前面的字符(这里是**\\d**)一次或多次。

因此,这个正则表达式可以用来检查字符串是否以数字开头,如果是的话,就匹配这些数字。例如,对于字符串"123abc",这个正则表达式会匹配到"123"。

正则表达式在字符串操作中非常强大,但也需要谨慎使用,因为复杂的正则表达式可能会导致性能问题或难以维护的代码。

以上就是一些常用的字符串操作和高级技巧,希望对你有所帮助!

好啦,今天的学习就到这里了,明天我们将要学习列表的高级操作(增删改查)。那小伙伴们,我们明天再见啦!

举报

相关推荐

0 条评论