【问题标题】:Separate a string with space用空格分隔字符串
【发布时间】:2013-08-07 14:44:17
【问题描述】:

我见过各种拆分字符串的方法。我已经从this 帖子中尝试过。

我正在尝试读取并拆分下一个字符串:{2b 00 00}

我看到最常见的情况是用“:”分隔一条消息,但是在这种情况下,我的消息是用空格分隔的。

尝试两种方式,使用常规的 split() 函数或使用 StringTokenizer 我得到一个“nullpointerexception”,我认为这是由于空间的原因:

private String splitReceivedString(String s) {
    String[] separated = s.split(" ");
    return separated[1];
}

我怎样才能得到这种字符串的值?

添加了可能存在问题的代码

在检查了您的一些答案后,我确实意识到问题来自蓝牙输入流。我从中得到空值。所以,这是我用来接收消息的代码:

代码与 bluetoothChat 示例几乎相同。但是为了适应我的程序,它被修改了,所以我可能有什么问题。

我有一个 MCU,当我向它发送另一个字符串时,它会返回这个字符串 {2b 00 00}。我认为这是在connectedThread

public class ConnectedThread extends Thread {

   public void run() {
    byte[] buffer = new byte[1024];  // buffer store for the stream
    int bytes; // bytes returned from read()

    /**Keep listening to the InputStream until an exception occurs*/
    while (true) {
        try {
            /**Read from the InputStream*/
            bytes = GlobalVar.mmInStream.read(buffer);

            /**Send the obtained bytes to the UI activity*/
            GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_READ, bytes, -1, buffer).sendToTarget();

所以,这是将字符串发送给主活动中的处理函数:

    public final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case GlobalVar.MESSAGE_STATE_CHANGE:
                //The code here is irelevant
            case  GlobalVar.MESSAGE_WRITE:
                byte[] writeBuf = (byte[]) msg.obj;
                /**construct a string from the buffer*/
                String writeMessage = new String(writeBuf);
                GlobalVar.mCommunicationArrayAdapter.add(writeMessage);
                break;
            case  GlobalVar.MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
                /**construct a string from the valid bytes in the buffer*/
                String readMessage = new String(readBuf);
                GlobalVar.mCommunicationArrayAdapter.add(readMessage);
                GlobalVar.readString = readMessage;                    
                break;

那么,变量GlobalVar.readString 就是我在拆分函数中得到的变量:

    private String splitReceivedString (String s) {

        String[] separated = s.split(" ");
        return separated[1];
    }

    receive1 = splitReceivedString (GlobalVar.readString);

所以,问题是它没有正确读取接收到的字符串,我不知道如何修复它。

【问题讨论】:

  • 我敢打赌你将null 传递给方法。
  • @arshajii 你什么意思?我已经阅读了我想用祝酒词分割的字符串,并且非常准确 2b 00 00
  • @arshajii 是正确的,我测试了这个方法并且效果很好。您传入的字符串与 {2b 00 00} 不同,或者您传入的是 null。
  • 显然不是,因为如果是,您将不会得到该异常。
  • 使用调试器来确保你传递的是什么

标签: java string split


【解决方案1】:

如果你得到一个NullPointerException,这不可能是由于你传递给 split 函数的字符串" " - 这绝对不是空的。无论出于何种原因,您作为splitReceivedString 方法的参数获得的String s 似乎都是null

【讨论】:

  • 嗯,是的,它可能是。我正在开发一个基于 bluetoothChat 示例的蓝牙应用程序。我在那里插入的字符串是我通过蓝牙接收的字符串,所以我从处理程序的变量readString 中获取它。我认为这是正确的,因为在向这个变量敬酒时,我在应用程序中看到了字符串2b 00 00。那么,我能做些什么呢?
  • 这是因为使用 .split(" ") 拆分字符串不会返回任何内容,因为 " " 不是 .split 方法所需的正确正则表达式语法。因此是空指针。
  • @KBusc:split 永远不会返回null。也请看这里:stackoverflow.com/a/6902125/55787
  • @chiccodoro,在主要问题中添加了解释问题所在的代码。
【解决方案2】:

插入一些输出(System.out.println 或其他)进行调试

  • 你的 s 是什么

  • 分开有多少个元素

  • 什么是分开的[0](如果你只有一个元素)

尝试使用

s.split("\\s");

在任何空白处分割。

【讨论】:

  • “s”变量是一个字符串,正如我在@chiccodoro 的蓝牙输入流的回答中所说的那样
  • TM,在主要问题中添加了解释问题所在的代码。
【解决方案3】:

您想使用正则表达式来拆分字符串。 试试这样

String splitString = "2b 00 00";
String delim = "[ ]";
//here we split the string wherever we see a character matching
//the inside of our square brackets. In this case a space
String[] splitResults = splitString.split(delim);

我已经对此进行了测试,它可以提供以下结果

splitResults[0] = "2b"
splitResults[1] = "00"  
splitResults[2] = "00"

【讨论】:

  • 这种方式也没有。我认为这是因为蓝牙输入流中的readString 变量。
  • 在主要问题中添加了代码,解释了问题所在。
【解决方案4】:

我在控制台应用程序中测试了您的代码。我把双引号改成了单引号,“Split”方法出现拼写错误

    static void Main(string[] args)
    {
        splitReceivedString("2b 00 00"); //1
    }

    public static string splitReceivedString(String s)
    {
        string[] separated = s.Split(' '); //2
        return separated[1];
    }

【讨论】:

  • 如果我设置单个 cuotes 我得到这个错误:The method split(String) in the type String is not applicable for the arguments (Char)
  • 因为''不是正则表达式语法
  • @beaumondo,在主要问题中添加了解释问题所在的代码。
猜你喜欢
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
相关资源
最近更新 更多