Java String1
程序员文章站
2023-12-24 16:29:03
...
import java.util.regex.Pattern;
public class Chapter6_3
{
public static void main(String[] args)
{
// StringAndStringBuilder();
// CharacterUnicode();
// SBDelete();
//testEncryptUncrypt();
test2("31233213");
}
public static void test1()
{
// 正则表达式判断指定的变量是否是合法的E-mail地址
String regex = "\\w{0,}\\@\\w{0,}\\.{1}\\w{0,}";
String str1 = "[email protected]";
String str2 = "aaaa";
String str3 = "[email protected]";
if(str1.matches(regex))
System.out.println(str1 + "是一个邮箱地址");
if(str2.matches(regex))
System.out.println(str2 + "是一个邮箱地址");
if(str3.matches(regex))
System.out.println(str3 + "是一个邮箱地址");
else
System.out.println("都不是邮箱地址");
// 计算文章中汉字的个数
String text = "明日科技soft";
int amount = 0;
for(int i = 0; i < text.length(); i++)
{
boolean matches = Pattern.matches("^[\u4E00-\u9FA5]{0,}$", ""
+ text.charAt(i));
if(matches)
amount++;
}
System.out.println(text + "中有几个汉字:" + amount + "个");
}
public static void StringAndStringBuilder()
{
String str = "";
long startTime = System.currentTimeMillis();
for(long i = 0; i < 10000; i++)
str = str + i;
long endTime = System.currentTimeMillis();
long time = endTime - startTime;
System.out.println("String消耗时间:" + time);
StringBuilder builder = new StringBuilder("");
startTime = System.currentTimeMillis();
for(long i = 0; i < 10000; i++)
builder.append(i);
endTime = System.currentTimeMillis();
time = endTime - startTime;
System.out.println("StringBuilder消耗时间:" + time);
}
public static void CharacterUnicode()
{
String text = "明日soft";
char arr[] = text.toCharArray();
StringBuilder builder = new StringBuilder();
for(char c : arr)
builder.append((int)c + " ");
System.out.println("\"明日soft\"的Unicode码是:");
System.out.println(builder.toString());
}
// 去掉字符串中重复的字符
public static void SBDelete()
{
String s = "命运如同海风-吹着青春的舟,飘摇的,曲折的,渡过了时间的海";
StringBuilder sb = new StringBuilder(s);
System.out.println(sb);
System.out.println("length = " + sb.length());
for(int i = 0; i < sb.length(); i++)
{
for(int j = i + 1; j < sb.length(); j++)
{
if(sb.charAt(i) == sb.charAt(j))
sb.deleteCharAt(j);
}
}
System.out.println(sb);
System.out.println("length = " + sb.length());
}
// 对value加密,secret密文字符
public static String EncryptUncrypt(String value, char secret)
{
byte bt[] = value.getBytes(); // 转换为字节数组
for(int i = 0; i < bt.length; i++)
bt[i] = (byte)(bt[i] ^ (int)secret); // 通过异或运算进行加密
return new String(bt, 0, bt.length);
}
public static void testEncryptUncrypt()
{
String value = "liangjisheng";
char secret = '祈';
System.out.println("加密前:" + value);
String encrypt = EncryptUncrypt(value, secret);
System.out.println("加密后:" + encrypt);
String uncrypt = EncryptUncrypt(encrypt, secret);
System.out.println("解密后:" + uncrypt);
}
// 验证字符串是否是回文
public static void test2(String str1)
{
StringBuilder str2 = new StringBuilder(str1);
str2.reverse();
int count = 0;
for(int i = 0; i < str1.length(); i++)
{
if(str1.charAt(i) != str2.charAt(i))
{
System.out.println(str1 + "不是回文字符串");
break;
}
if(str1.charAt(i) == str1.charAt(i))
count++;
}
if(count == str1.length())
System.out.println(str1 + "是回文字符串");
}
}