如何在java中删除空白后的字符串中的任何内容

如何在java中删除空白后的字符串中的任何内容,java,android,Java,Android,我有这样一个字符串:12/16/2011 12:00:00 AM 现在我只想在Textview上显示日期部分,即2011年12月16日 然后拆下另一部分。我需要为此做些什么 任何帮助都将受到感谢 谢谢。使用java.text.DateFormat将字符串解析为日期,然后重新格式化以使用其他日期格式显示: DateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); inputFormat.setLenient(fa

我有这样一个字符串:
12/16/2011 12:00:00 AM

现在我只想在Textview上显示日期部分,即2011年12月16日 然后拆下另一部分。我需要为此做些什么

任何帮助都将受到感谢
谢谢。

使用java.text.DateFormat将字符串解析为日期,然后重新格式化以使用其他日期格式显示:

DateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");
outputFormat.setLenient(false);
Date d = inputFormat.parse("12/16/2011 12:00:00 AM");
String s = outputFormat.format(d);

使用java.text.DateFormat将字符串解析为日期,然后重新格式化以使用另一种日期格式显示:

DateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");
outputFormat.setLenient(false);
Date d = inputFormat.parse("12/16/2011 12:00:00 AM");
String s = outputFormat.format(d);

只有两种简单的可能性:

String str = "12/16/2011 12:00:00 AM";

// method 1: String.substring with String.indexOf
str.substring(0, str.indexOf(' '));

// method 2: String.split, with limit 1 to ignore everything else
str.split(" ", 1)[0];

只有两种简单的可能性:

String str = "12/16/2011 12:00:00 AM";

// method 1: String.substring with String.indexOf
str.substring(0, str.indexOf(' '));

// method 2: String.split, with limit 1 to ignore everything else
str.split(" ", 1)[0];

使用正则表达式(比其他表达式更健壮-即使没有找到空白也有效)


使用正则表达式(比其他表达式更健壮-即使没有找到空白也有效)


您可以使用下面的代码来获取子字符串

String thisString="Hello world";

String[] parts = theString.split(" ");
String first = parts[0];//"hello"
String second = parts[1];//"World"

您可以使用下面的代码来获取子字符串

String thisString="Hello world";

String[] parts = theString.split(" ");
String first = parts[0];//"hello"
String second = parts[1];//"World"

请收回您的评论,我只是尝试了代码,它确实工作。。。当然,你需要使用函数calloh的结果。很抱歉,这是我的错误,我没有打印结果。请收回你的评论,我只是尝试了代码,它确实有效。。。当然,您需要使用函数calloh的结果。很抱歉,这是我的错误,我没有打印结果。
String thisString="Hello world";

String[] parts = theString.split(" ");
String first = parts[0];//"hello"
String second = parts[1];//"World"