如果我在.txt文件中有以下标题:
CUSTOMER PAYMENT ERR CORR PAYMENT AMOUNT PAYMENT
REFERENCE NUMBER INSTRUCTION TYPE REASON* TYPE**
如何使用以下元素创建List<string>
:
CUSTOMER REFERENCE NUMBER
,PAYMENT INSTRUCTION TYPE
,ERR CORR REASON*
,PAYMENT AMOUNT
,PAYMENT TYPE**
另一个例子:
CUSTOMER PAYMENT ERR CORR PAYMENT AMOUNT PAYMENT
REFERENCE NUMBER INSTRUCTION TYPE TYPE**
会给我:
CUSTOMER REFERENCE NUMBER
,PAYMENT INSTRUCTION TYPE
,ERR CORR
,PAYMENT AMOUNT
,PAYMENT TYPE**
注意,标题间隔有多个空格&gt; = 2,而一个标题中的每个字由一个空格分隔。只有第一行才能写标题。
答案 0 :(得分:2)
这是一种方式。首先,&#34;合并&#34;两条线在一起。我假设它们的长度相同。例如:
if (imagesPathList != null) {
if (imagesPathList.size() >= 1) {
for (int i = 0; i < imagesPath.length; i++) {
String strImg = imagesPath[i];
File f = new File("" + strImg);
Log.e("File", "" + f);
doFileUpload(f);
Log.d("saveimages", "" + imagesPathList);
}
Toast.makeText(
MainActivity.this,
imagesPathList.size()
+ " no of images are selected",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(
MainActivity.this,
imagesPathList.size() + " no of image are selected",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, " no images are selected",
Toast.LENGTH_SHORT).show();
}
break;
}
}
public void doFileUpload(File f) {
FTPClient client = new FTPClient();
try {
client.connect(FTP_HOST, 21);
Log.e("clientconnect", "" + client);
client.login(FTP_USER, FTP_PASS);
Log.e("clientlogin", "" + client);
client.setType(FTPClient.TYPE_BINARY);
Log.e("clienttype", "" + client);
client.changeDirectory("/ramesh2/");
Log.i("", "$$$$$$$$$$$$$$$$$" + ("/ramesh2/"));
// int reply = client.getReplyCode();
client.upload(f, new MyTransferListener());
// Log.e("filenameupload", "" + photoFile);
Log.e("clientupload", "" + client);
// Log.e("file",""+fileName);
} catch (Exception e) {
e.printStackTrace();
try {
client.disconnect(true);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public class MyTransferListener implements FTPDataTransferListener {
public void started() {
// btn.setVisibility(View.GONE);
// Transfer started
Toast.makeText(getApplicationContext(), " Upload Started ...",
Toast.LENGTH_SHORT).show();
// System.out.println(" Upload Started ...");
}
public void transferred(int length) {
// Yet other length bytes has been transferred since the last time
// this
// method was called
Toast.makeText(getApplicationContext(),
" transferred ..." + length, Toast.LENGTH_SHORT).show();
// System.out.println(" transferred ..." + length);
}
public void completed() {
// btn.setVisibility(View.VISIBLE);
// Transfer completed
Toast.makeText(getApplicationContext(), " completed ...",
Toast.LENGTH_SHORT).show();
// System.out.println(" completed ..." );
}
public void aborted() {
// btn.setVisibility(View.VISIBLE);
// Transfer aborted
Toast.makeText(getApplicationContext(),
" transfer aborted , please try again...",
Toast.LENGTH_SHORT).show();
// System.out.println(" aborted ..." );
}
public void failed() {
// btn.setVisibility(View.VISIBLE);
// Transfer failed
System.out.println(" failed ...");
}
// Jibble.
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_MULTIPLE
&& resultCode == Activity.RESULT_OK && null != data) {
// uri=data.getData();
// System.out.println("Current image Path is ----->" +
// getRealPathFromURI(uri));
imagesPathList = new ArrayList<String>();
imagesPath = data.getStringExtra("data").split("\\|");
try {
lnrImages.removeAllViews();
} catch (Throwable e) {
e.printStackTrace();
}
for (int i = 0; i < imagesPath.length; i++) {
Log.e("imagesPath can", "" + imagesPath);
imagesPathList.add(imagesPath[i]);
Log.w("imagesPathList are", "" + imagesPathList);
yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
Log.d("yourbitmap is", "" + yourbitmap);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(yourbitmap);
imageView.setAdjustViewBounds(true);
lnrImages.addView(imageView);
String listString = "";
for (String s : imagesPathList) {
listString += s + "\t";
}
j = listString.toString();
uri = Uri.parse(j);
r = uri.toString();
Log.d("mnmnmnmnmnmnmhjjuigyigsuiagducfuducgfasicfgds", "" + r);
Log.d("anananananananananananananananananananananannananand",
"" + uri);
这可以让你:
var s1 = " CUSTOMER PAYMENT ERR CORR PAYMENT AMOUNT PAYMENT ";
var s2 = " REFERENCE NUMBER INSTRUCTION TYPE REASON* TYPE** ";
var merged = new string(Enumerable.Range(0, s1.Length)
.Select(t=>s1[t]!=' ' ? s1[t] : s2[t]).ToArray());
然后,查找该字符串中双(或更多)空格的索引:
REFCUSTOMERUMBER INSTPAYMENT TYPE ERRACORR PAYMENT AMOUNT PAYMENT
然后,使用这些索引来剪切两个字符串并将它们连接起来,修剪掉空格。我明确地添加了字符串的开头和结尾,以防两端没有双空格。
var indexes = Regex.Matches(merged, @" +").OfType<Match>().Select(t=>t.Index).ToList();
这可以获得输出:
indexes.Insert(0, 0);
indexes.Add(merged.Length-1);
indexes.Sort(); // not sure if Regex.Matches will necessarily be in order
var columns = new List<string>();
for (int i=0; i<indexes.Count-1;i++)
{
var column = (
s1.Substring(indexes[i], indexes[i+1]-indexes[i]).Trim() + " " +
s2.Substring(indexes[i], indexes[i+1]-indexes[i]).Trim()
).Trim();
if (column != string.Empty)
columns.Add(column);
}
here是一个有效的例子。
答案 1 :(得分:0)
如果txt的格式与你说的一样,这里是一个演示
string[] arr = {
@" CUSTOMER PAYMENT ERR CORR PAYMENT AMOUNT PAYMENT
REFERENCE NUMBER INSTRUCTION TYPE REASON* TYPE**",
@" CUSTOMER PAYMENT ERR CORR PAYMENT AMOUNT PAYMENT
REFERENCE NUMBER INSTRUCTION TYPE TYPE**"
};
foreach (string str in arr)
{
List<string> list = new List<string>();
var tmp = Regex.Split(str.Trim(), @"\s{2,}");
list.Add(tmp[0] + " " + tmp[5]);
list.Add(tmp[1] + " " + tmp[6]);
if (tmp.Length == 9)
{
list.Add(tmp[2] + " " + tmp[7]);
}
else
{
list.Add(tmp[2]);
}
list.Add(tmp[3]);
list.Add(tmp[4] + " " + tmp[tmp.Length - 1]);
Console.WriteLine(string.Join(",", list));
}