Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将字符串拆分为子字符串?_Java_Android_String - Fatal编程技术网

Java 如何将字符串拆分为子字符串?

Java 如何将字符串拆分为子字符串?,java,android,string,Java,Android,String,我想将字符串“KD-435”拆分为两个子字符串,以检查第一个子字符串“KD-”是否以以下字符开头“KD-”,第二个子字符串是数字beteewn“400-500” 我有下面的方法,我想在这个位置改变它,如果(ssid.startsWith(“KD-”)) private void check\u wifi\u available(){ WifiManager WifiManager=(WifiManager)此 .getSystemService(此.WIFI_服务); 最终列表结果=wifiMa

我想将字符串
“KD-435”
拆分为两个子字符串,以检查第一个子字符串
“KD-”
是否以以下字符开头
“KD-”
,第二个子字符串是数字beteewn
“400-500”

我有下面的方法,我想在这个位置改变它,如果(ssid.startsWith(“KD-”))

private void check\u wifi\u available(){
WifiManager WifiManager=(WifiManager)此
.getSystemService(此.WIFI_服务);
最终列表结果=wifiManager.getScanResults();
如果(结果!=null){
List updatedResults=new ArrayList();
//选择以这些“KD”字符开头的wifi接入网桥。
对于(int i=0;i0){
字符串a=CalculateBaseAccessPoint(updatedResults);
text wifi.setText(a.toString());
}
}
}
String[]parts=ssid.split(“-”);
如果(parts.length==2)
{
String firstPart=parts[0];//这是KD
String secondPart=parts[1];//这是435

如果(firstPart.equals(“KD”)&&Integer.parseInt(secondPart)>=400&&Integer.parseInt(secondPart)您不一定需要显式拆分它(在调用
String.split
的意义上),例如

if(s.startsWith(“KD-”){
intv=Integer.parseString(s.substring(3));

如果(v>=400&&v您可以使用正则表达式一次性完成所有操作:

Pattern p = Pattern.compile("^KD-(4[0-9]{2}|500)$");
Matcher m = p.matcher("KD-411"); // Replace with your string.
if (m.matches()) {
    // It worked!
} else {
    // It didn't.
}

你是指子字符串吗?是的,我是指子字符串^^typo。你能给我解释一下这个正则表达式“^KD-(4[0-9]{2}500)$”吗?它包含400-500之间的所有数字吗?这里的数字4“4[0-9]”是什么意思?
4[0-9]{2}
表示“4后面有两个数字,范围为0-9”,即这与数字400-499匹配。
4[0-9]{2}|500
匹配数字400-499或500,即400-500。
^KD-
表示“以KD开头--”,并且,
$
表示没有任何东西跟在400-500范围内的数字后面。正是这样!我认为正则表达式有点有趣--它有点像一个拼图,能够精确匹配您想要的字符串。它们经常会派上用场。如果您有兴趣学习如何读写它们。
    String [] parts = ssid.split("-");

if(parts.length == 2)
{

    String firstPart = parts[0]; //That's KD
    String secondPart = parts[1]; //That's 435

    if(firstPart.equals("KD")&&Integer.parseInt(secondPart)>=400&&Integer.parseInt(secondPart)<=500)
    {
    //do whatever you want
    }
}
if (s.startsWith("KD-")) {
  int v = Integer.parseString(s.substring(3));
  if (v >= 400 && v <= 500) {
    // Do whatever.
  }
}
Pattern p = Pattern.compile("^KD-(4[0-9]{2}|500)$");
Matcher m = p.matcher("KD-411"); // Replace with your string.
if (m.matches()) {
    // It worked!
} else {
    // It didn't.
}