運(yùn)算符和字符串操作在程序設(shè)計中非常常用。本篇文章將會從多個方面闡述這一主題。
一、運(yùn)算符
運(yùn)算符是一種用來執(zhí)行特定數(shù)學(xué)或邏輯運(yùn)算的符號。Python中的運(yùn)算符包括:算數(shù)運(yùn)算符、比較運(yùn)算符、賦值運(yùn)算符、邏輯運(yùn)算符、位運(yùn)算符等多種類型。
1.1 算數(shù)運(yùn)算符
算數(shù)運(yùn)算符是最基本的運(yùn)算符之一,它們用于執(zhí)行基本的算術(shù)操作,如加、減、乘、除等。
a = 10
b = 3
print("a + b =", a + b) # 輸出a + b = 13
print("a - b =", a - b) # 輸出a - b = 7
print("a * b =", a * b) # 輸出a * b = 30
print("a / b =", a / b) # 輸出a / b = 3.3333333333333335
print("a // b =", a // b) # 輸出a // b = 3
print("a % b =", a % b) # 輸出a % b = 1
print("a ** b =", a ** b) # 輸出a ** b = 1000
1.2 比較運(yùn)算符
比較運(yùn)算符用于比較兩個變量的值,并返回一個布爾值(True或False)。
a = 10
b = 5
print("a > b =", a > b) # 輸出a > b = True
print("a < b =", a < b) # 輸出a < b = False
print("a == b =", a == b) # 輸出a == b = False
print("a != b =", a != b) # 輸出a != b = True
print("a >= b =", a >= b) # 輸出a >= b = True
print("a <= b =", a <= b) # 輸出a <= b = False
1.3 賦值運(yùn)算符
賦值運(yùn)算符用于將右邊的值賦給左邊的變量。
a = 10
b = 5
c = a + b
print("c =", c) # 輸出c = 15
a += b
print("a += b:a =", a) # 輸出a += b:a = 15
a -= b
print("a -= b:a =", a) # 輸出a -= b:a = 10
a *= b
print("a *= b:a =", a) # 輸出a *= b:a = 50
a /= b
print("a /= b:a =", a) # 輸出a /= b:a = 10.0
a %= b
print("a %= b:a =", a) # 輸出a %= b:a = 0.0
a **= b
print("a **= b:a =", a) # 輸出a **= b:a = 0.0
1.4 邏輯運(yùn)算符
邏輯運(yùn)算符用于組合多個條件,并返回一個布爾值。
a = 10
b = 5
c = 3
print("a > b and b > c =", a > b and b > c) # 輸出a > b and b > c = True
print("a > b or b < c =", a > b or b < c) # 輸出a > b or b < c = True
print("not a > b =", not a > b) # 輸出not a > b = False
1.5 位運(yùn)算符
位運(yùn)算符是用來執(zhí)行位運(yùn)算的運(yùn)算符。
a = 10
b = 5
print("a & b =", a & b) # 輸出a & b = 0
print("a | b =", a | b) # 輸出a | b = 15
print("a ^ b =", a ^ b) # 輸出a ^ b = 15
print("~a =", ~a) # 輸出~a = -11
print("a << 2 =", a << 2) # 輸出a << 2 = 40
print("a >> 2 =", a >> 2) # 輸出a >> 2 = 2
二、字符串操作
字符串是程序中常用的數(shù)據(jù)類型之一。在Python中,字符串可以使用單引號、雙引號或三引號來表示。字符串操作是對字符串進(jìn)行操作的一些方法,包括格式化、拆分、連接、替換、大小寫轉(zhuǎn)換等。
2.1 字符串格式化
字符串格式化是指將變量的值插入到占位符中,以便動態(tài)生成字符串。
name = "Tom"
age = 18
print("My name is %s, I am %s years old." % (name, age)) # 輸出My name is Tom, I am 18 years old.
2.2 字符串拆分
字符串拆分是指將字符串按照指定的分隔符進(jìn)行拆分。
string = "a,b,c,d,e"
list = string.split(",")
print(list) # 輸出['a', 'b', 'c', 'd', 'e']
2.3 字符串連接
字符串連接是指將多個字符串拼接成一個字符串。
string1 = "hello"
string2 = "world"
string = string1 + " " + string2
print(string) # 輸出hello world
2.4 字符串替換
字符串替換是指將字符串中的某些字符替換成其他字符。
string = "hello world"
new_string = string.replace("world", "Python")
print(new_string) # 輸出hello Python
2.5 字符串大小寫轉(zhuǎn)換
字符串大小寫轉(zhuǎn)換是指將字符串中的字母大小寫進(jìn)行轉(zhuǎn)換。
string = "Hello World"
lower_string = string.lower()
upper_string = string.upper()
print(lower_string) # 輸出hello world
print(upper_string) # 輸出HELLO WORLD
三、Java程序中常用的運(yùn)算符和字符串操作
Java語言是一種廣泛使用的面向?qū)ο缶幊陶Z言,同樣也包含了多種運(yùn)算符和字符串操作。
3.1 運(yùn)算符
Java中的運(yùn)算符和Python中的運(yùn)算符類似,包括算數(shù)運(yùn)算符、比較運(yùn)算符、賦值運(yùn)算符、邏輯運(yùn)算符、位運(yùn)算符等多種類型。
3.2 字符串操作
Java中的字符串操作與Python中的字符串操作也非常相似,包括字符串格式化、拆分、連接、替換、大小寫轉(zhuǎn)換等。
3.3 代碼示例
public class Main {
public static void main(String[] args) {
// 算數(shù)運(yùn)算符
int a = 10;
int b = 3;
System.out.println("a + b = " + (a + b)); // 輸出a + b = 13
System.out.println("a - b = " + (a - b)); // 輸出a - b = 7
System.out.println("a * b = " + (a * b)); // 輸出a * b = 30
System.out.println("a / b = " + (a / b)); // 輸出a / b = 3
System.out.println("a % b = " + (a % b)); // 輸出a % b = 1
System.out.println("a += b:a = " + (a += b)); // 輸出a += b:a = 13
System.out.println("a -= b:a = " + (a -= b)); // 輸出a -= b:a = 10
System.out.println("a *= b:a = " + (a *= b)); // 輸出a *= b:a = 30
System.out.println("a /= b:a = " + (a /= b)); // 輸出a /= b:a = 10
System.out.println("a %= b:a = " + (a %= b)); // 輸出a %= b:a = 1
// 比較運(yùn)算符
int c = 5;
System.out.println("a > b = " + (a > b)); // 輸出a > b = true
System.out.println("a < b = " + (a < b)); // 輸出a < b = false
System.out.println("a == b = " + (a == b)); // 輸出a == b = false
System.out.println("a != b = " + (a != b)); // 輸出a != b = true
System.out.println("a >= b = " + (a >= b)); // 輸出a >= b = true
System.out.println("a <= b = " + (a <= b)); // 輸出a <= b = false
// 邏輯運(yùn)算符
System.out.println("a > b && b > c =" + (a > b && b > c)); // 輸出a > b && b > c = true
System.out.println("a > b || b < c =" + (a > b || b < c)); // 輸出a > b || b < c = true
System.out.println("!(a > b) =" + !(a > b)); // 輸出!(a > b) = false
// 位運(yùn)算符
int d = 5;
System.out.println("a & b = " + (a & b)); // 輸出a & b = 1
System.out.println("a | b = " + (a | b)); // 輸出a | b = 15
System.out.println("a ^ b = " + (a ^ b)); // 輸出a ^ b = 14
System.out.println("~a = " + (~a)); // 輸出~a = -31
System.out.println("a << 2 = " + (a << 2)); // 輸出a << 2 = 40
System.out.println("a >> 2 = " + (a >> 2)); // 輸出a >> 2 = 2
// 字符串格式化
String name = "Tom";
int age = 18;
System.out.printf("My name is %s, I am %d years old.", name, age); // 輸出My name is Tom, I am 18 years old.
// 字符串拆分
String str = "a,b,c,d,e";
String[] array = str.split(",");
System.out.println(Arrays.toString(array)); // 輸出[a, b, c, d, e]
// 字符串連接
String str1 = "hello";
String str2 = "world";
String string = str1 + " " + str2;
System.out.println(string); // 輸出hello world
// 字符串替換
String oldString = "hello world";
String newString = oldString.replace("world", "Java");
System.out.println(newString); // 輸出hello Java
// 字符串大小寫轉(zhuǎn)換
String s = "Hello World";
String lowStr = s.toLowerCase();
String upStr = s.toUpperCase();
System.out.println(lowStr); // 輸出hello world
System.out.println(upStr); // 輸出HELLO WORLD
}
}