2014-02-12 147 views
0

这次我有一个简单的问题,我猜...
我是Java/Android的新手,很抱歉。更换字符后在两个字符中分隔字符串

我有以下字符串:

String Column = Product_One_60; 
String ColumnTwo = Column.replace("_"," "); 

这给了我这样的:

//ColumnTwo = Product One 60 

到目前为止好,然后我需要两个字符串这样的:

String Product = Product One; 
String Content = 60; 

我需要做些什么来获得?

+1

我想'Product_One_60'是一个变量,字符串常量由双引号一样'“_”'包围。 –

+0

请遵循java命名约定 - 变量以小写字母开头。 – csmckelvey

+0

我不敢相信它真的有效。不像你写的那样。 –

回答

0

这应该是你最初的想法的翻译工作:

String Column = "Product_One_60"; 
String[] parts = Column.split("_"); 

String Product = parts[0] + " " + parts[1]; // "Product One" 
String Content = parts[2];     // "60" 
0

您可以使用字符串分割功能。它会将一个字符串拆分成存储在字符串数组中的部分。所使用的分隔符是字符串中的特定字符,因此您必须将“_”替换为要用来分隔字符串的字符串。

例如:(假设你使用“&”作为分隔符)

String product = "Product_One&60"; 
String array = product.split("&"); 

System.out.print(array[0]);//"Product_One" 
System.out.print(array[1]);//"60"