简体   繁体   English

检查字符串是否以特定字母开头或结尾-Java

[英]Check if string starts or ends with specific letter - java

I have a little problem checking if a string have a letter in it. 我在检查字符串中是否有字母时遇到一些问题。 Right now, i have a server/client situation where the server sends back a command to the client. 现在,我遇到服务器/客户端的情况,服务器将命令发送回客户端。

That command can be: 该命令可以是:

RM20 B
RM20 L
RM20 I

RM20 A "User input"

RM20 C

How do i check the letter if its a B, L, I or A or C?? 我如何检查字母是B,L,I还是A或C?

Right now i made this code: 现在我做了这段代码:

if ((fromServer.startsWith("RM20 A"))) {

else if ((fromServer.startsWith("RM20 C")))

else {

}

does this work? 这有效吗? or will the if statement and else if statement be the same? 还是if语句和else if语句相同? i can't use ends with, because the RM20 A have an input in the end which can be alot. 我不能使用末端为,因为RM20 A末端有很多输入。

is there a way to check whats in the middle. 有没有办法检查中间的东西。 when you get, lets says 当你得到时,说

"RM20 A 123213124" as a String? “ RM20 A 123213124”作为字符串?

Assuming your format is something like: 假设您的格式如下:

 <RM Code> <Letter> <Numbers> <Anything else>

Then you could do: 然后,您可以执行以下操作:

 String[] values = input.split(" ");

 if(values[1].equals("A")) {
     // Code for A.
 }

I would only use the startsWith() test for the "A" command input, since that is the only one that has an arbitrary ending. 我只会对“ A”命令输入使用startsWith()测试,因为那是唯一具有任意结尾的命令。 The other commands have exact values, so you should use an equals() test for those. 其他命令具有确切的值,因此您应该使用equals()测试。 This way, you can reliably catch errors (eg, "RM20 B xyz"). 这样,您可以可靠地捕获错误(例如“ RM20 B xyz”)。

static final String A_COMMAND_PREFIX = "RM20 A ";

.
.
.

if (fromServer.startsWith(A_COMMAND_PREFIX)) {
  // Process user input
  final String userInput = fromServer.substring(A_COMMAND_PREFIX.length());

} else if (fromServer.equals("RM20 B")) {
  // Process B command
} else if (fromServer.equals("RM20 L")) {
  // Process L command
} else if (fromServer.equals("RM20 I")) {
  // Process I command
} else if (fromServer.equals("RM20 C")) {
  // Process C command
} else {
  // Handle unknown command
}

I assume you have already determined that fromServer is not null. 我假设您已经确定fromServer不为null。

Use regex to capture different groups. 使用正则表达式捕获不同的组。 The String regex expression captures any word character up until a space. String regex表达式捕获直到空格为止的所有单词字符。 Then it captures any letter az or AZ. 然后捕获任何字母az或AZ。 Then, if there's a space and some other input, it captures that and uses it. 然后,如果有空间和其他一些输入,它将捕获并使用它。

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class test {
    public static void main(String ar[]) throws java.io.IOException {       

        String regex = "(\\w*) ([a-zA-Z])( (.*))?";

        String str1 = "RM20 A";
        String str2 = "RM20 B";
        String str3 = "RM20 C this is a user message.";
        Pattern pattern = Pattern.compile(regex);

        Matcher match1 = pattern.matcher(str1);
        Matcher match2 = pattern.matcher(str2);
        Matcher match3 = pattern.matcher(str3);

        String a;
        while (match1.find()) {          
            a = "Group 1: " + match1.group(1) + "\nGroup 2: " + match1.group(2);  
            System.out.println(a);        
        }  

        System.out.println();

        while (match2.find()) {          
            a = "Group 1: " + match2.group(1) + "\nGroup 2: " + match2.group(2);  
            System.out.println(a);
        }   

        System.out.println();

        while (match3.find()) {          
            a = "Group 1: " + match3.group(1) + "\nGroup 2: " + match3.group(2) + "\nGroup 4: " + match3.group(4);  
            System.out.println(a);       
        }
    }
}

>>> Group 1: RM20
>>> Group 2: A

>>> Group 1: RM20
>>> Group 2: B

>>> Group 1: RM20
>>> Group 2: C
>>> Group 4: this is a user message.

You could also capture the letters/number in the input code: 您还可以在输入代码中捕获字母/数字:

    String regex = "([a-zA-Z]*)(\\d*) ([a-zA-Z])( (.*))?";

    String str1 = "RM20 A message thingy";
    Pattern pattern = Pattern.compile(regex);

    Matcher match1 = pattern.matcher(str1);
    while (match1.find()) {          
        System.out.println(match1.group(1));
        System.out.println(match1.group(2));
        System.out.println(match1.group(3));
        System.out.println(match1.group(5));
    }

>>> RM
>>> 20
>>> A
>>> message thingy

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除Java文件中以特定字符串开头和结尾的特定行? - How to delete the specific lines that starts and ends with specific string in a file in java? (Java)将以特定单词开头和结尾的每个字符串存储到数组中 - (Java) storing every string that starts and ends with a specific word into an array 如何使用Java在文本文件中复制以特定字符开头和结尾的字符串 - How to copy a string that starts and ends with specific characters in a text file using Java 用于检查Not的Java RegEx以特定字符串开头,但包含多个字符串 - Java RegEx to Check for Not starts with Specific String but contains multiple Strings 检查中间字符串是否以某种模式开始-结束并更新字符串 - Check if an intermediate string starts-ends with certain pattern and update the string Java在字符串中重复的字母检查 - Java repeated letter check in string 检查输入是否以数字开头并以大写字母结尾 - Checking if the input starts with a number and ends with an Upper letter java 中需要正则表达式模式,用于匹配字符串以“{{”开头并以“}}”结尾 - Regex Pattern required in java for matching string starts with '{{' and ends with “}}” 如何检查字符串是否以数字,拉丁字母或下划线开头 - How to check if string starts with a number, latin letter or underscore 如何检查字符串以Java中的另一个字符串开头 - how to check that string starts with another string in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM