import java.util.ArrayList;
import java.util.Random;
public class Tes {
/**
* @param args
*
*
*/
public void Test() {
String delims = "X";
String ss = "2008XXX8";
String x = "1";
String dd[] = ss.split(delims);
for (String me : dd) {
delims = x;
System.out.println(me);
}
}
public void test1() {
String testStr = "2008XXX8";
// System.out.println(testStr.substring(0, 8));
// 第几位字符替换
int position = 5;
if (testStr.length() > 2) {
testStr = testStr.substring(0, position - 1) + "A"
+ testStr.substring(position);
// System.out.println(testStr.substring(0, position-1));
// System.out.println( testStr.substring(position));
System.out.println(testStr);
}
}
public void Test2() {
String delims = "X";
String ss = "2008XXX8";
int count = ss.length() - ss.replace(delims, "").length(); //判断出现变量数
System.out.println(count);
System.out.println(ss.length());
System.out.println(ss.replace(delims, "").length());
for (int i = 0; i < 5; i++) {
Random rdm = new Random(); //创建随机对象
String x = null;
int c = (rdm.nextInt() >>> 1) % 10;//1---10
x = String.valueOf(c);
String dd = ss.replace(delims, x);//替换
System.out.println(dd);
for (int j = 1; j < count; j++) {
}
}
//字符窜复制
// String tt;
// tt= ss.strRep("aaa",5);
}
public void test3() {
String delims = "X";
String ss = "2008XXX8";
int count = ss.length() - ss.replace(delims, "").length(); //判断出现变量数
System.out.println(count);
System.out.println(ss.length());
System.out.println(ss.replace(delims, "").length());
for (int i = 0; i < 5; i++) {
Random rdm = new Random(); //创建随机对象
String x = null;
int c = (rdm.nextInt() >>> 1) % 10;//1---10
x = String.valueOf(c);
String dd = ss.replace(delims, x);//替换
System.out.println(dd);
for (int j = 1; j < count; j++) {
}
}
}
public void test4() {
String testStr = "2008XXX8";
String delims = "X";
for (int s = 0; s < 3; s++) {
for(int i = 0;i<2;i++)
{
if (testStr.length() > 2) {
int count = testStr.replace(delims, "").length();
int position = count;
if(position !=0)
{
Random rdm = new Random(); //创建随机对象
String x = null;
int c = (rdm.nextInt() >>> 1) % 10;//1---10
x = String.valueOf(c);
testStr = testStr.substring(0, position - 1) + x
+ testStr.substring(position);
}
}
System.out.println(testStr);
}
// if(position !=0)
// {
// for(int i=0;i<5;i++)
// {
// }
// }
}
}
public void test5()
{
for(int i=0;i<5;i++)
{
Random rd = new Random(); //创建随机对象
String n="";
//String num1="";
int rdGet; //取得随机数
do{
rdGet=Math.abs(rd.nextInt())%10+48; //产生48到57的随机数(0-9的键位值)
//rdGet=Math.abs(rd.nextInt())%26+97; //产生97到122的随机数(a-z的键位值)
char num1=(char)rdGet;
String dd=Character.toString(num1);
n+=dd;
}while(n.length()<3);//假如长度小于3
// System.out.println("请输入3位随机数:"+n);
String testStr = "2008XXX8";
String delims = "X";
int count = testStr.replace(delims, "").length();//判断第几个是X
int count1 = testStr.length() - testStr.replace(delims, "").length(); //判断出现变量数
int position = count;
testStr = testStr.substring(0, position - 1) + n
+ testStr.substring(7);
System.out.println(testStr);
}
}
public static void main(String[] args) {
Tes t = new Tes();
// t.Test();
// t.test1();
// t.Test2();
// t.test3();
// t.test4();
t.test5();
}
}
String 方法研究 替换 随机数
最新推荐文章于 2025-04-09 07:46:05 发布