简体   繁体   English

从(“ \\ n”)换行符获取字符串

[英]Get string from (“\n”) new line operator

I have an arraylist that containing string like abc"\\n"1234,cde"\\n"567 fgh"\\n"890. 我有一个包含类似于abc“ \\ n” 1234,cde“ \\ n” 567 fgh“ \\ n” 890这样的字符串的arraylist。 I want to get string from where it found new line. 我想从找到新行的地方获取字符串。 like from this array i want only 1234,567 and 890. here is what am doing. 像从这个数组我只想要1234567和890。这是在做什么。 but it says "Incompatible Types" Thanks actually i have show the dialog that containing these contacts.and than if user click on any contacts it opens the dialer activity. 但它说“不兼容的类型”实际上,我已经显示了包含这些联系人的对话框。如果用户单击任何联系人,它将打开拨号程序活动。

 for(String contact: manycontacts){
        String contacts=contact.split("\\r?\\n");
    }

split returns a String[] not a String split返回String[]而不是String

for(String contact: manycontacts){
    String[] contacts=contact.split("\\r?\\n");
}

If you need it as one string you have to iterate over the array and concatinate the strings. 如果需要将其作为一个字符串,则必须遍历数组并隐含字符串。 or use commons-utils from apache to join it. 或使用apache的commons-utils加入。

split method returns an array of strings String[] . split方法返回字符串数组String[] In your code, you're returning a simply String. 在您的代码中,您将返回一个简单的String。 That is the incompatibility. 那就是不兼容性。

Simple change it to: 只需将其更改为:

 for(String contact: manycontacts){
        String[] contacts=contact.split("\\r?\\n");
 }

The split function returns an array of Strings, but you are trying to assign an array of strings to a single string, which throws this error. split函数返回一个字符串数组,但是您试图将一个字符串数组分配给单个字符串,这将引发此错误。 Rather use: 而是使用:

for(String contact: manycontacts){
    String[] contacts=contact.split("\\r?\\n");
    for(String split : contacts) {
       //here you can go on and workt with a single splittet value
    }
}

contact.split() is returning a String array, not a String, hence the type is incompatible. contact.split()返回的是String数组,而不是String,因此类型不兼容。 You'll need 你需要

String[] contacts = contact.split("\\r?\\n");

这是因为split返回的String数组将为String []。

简单地我用this.trim调整字符串。

 String numberonly = contacts.substring(contact.indexOf('\n'));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM