尝试使用Javascript,我有一个很长的字符串,我需要使用在字符串中重复的单词将其拆分为数组。
示例:
long_string = "THS& | Willam | Baker | 1234 Corker St| Jacksonville, TX 75074| THS& Steve | James | 4312 Corker St | Jacksonville, TX 75074| THS& | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|"
我尝试过拆分和匹配,但始终忽略了THS&
split_string = [];
split_string = long_string.split(/THS&/);
console.log(split_string);
放入数组:
[THS& | Willam | Baker | 1234 Corker St| Jacksonville, TX 75074|, THS& Steve | James | 4312 Corker St | Jacksonville, TX 75074|, THS& | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|]
但是我得到的是类似的东西
[| Willam | Baker | 1234 Corker St| Jacksonville, TX 75074|, Steve | James | 4312 Corker St | Jacksonville, TX 75074|, | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|]
答案 0 :(得分:5)
您在split
中匹配的任何内容(例如THS&
)均不包括在结果中。解决方案是使用look-ahead,它实际上不会捕获字符串:
var long_string = "THS& | Willam | Baker | 1234 Corker St| Jacksonville, TX 75074| THS& Steve | James | 4312 Corker St | Jacksonville, TX 75074| THS& | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|"
var split_string = long_string.split(/(?=THS&)/);
console.log(split_string);
答案 1 :(得分:2)
split()
方法在结果子字符串中不包含分隔符。如果分隔符总是相同,请考虑在每个子字符串的开头将其串联。 (您可以遍历split_string
并在数组的每个字符串的开头添加串联的"THS& "
)
答案 2 :(得分:1)
分隔符不会包含在结果中,因此您必须将其重新添加。而且,第一项将为空,因此请使用Array.prototype.shift()将其从结果数组中剥离。 EG:
var long_string = "THS& | Willam | Baker | 1234 Corker St| Jacksonville, TX 75074| THS& Steve | James | 4312 Corker St | Jacksonville, TX 75074| THS& | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|";
var split_string = long_string.split("THS&").map(function(item) {
return "THS&"+item;
});
split_string.shift();
console.log(split_string);
/*
[
"THS& | Willam | Baker | 1234 Corker St| Jacksonville, TX 75074| ",
"THS& Steve | James | 4312 Corker St | Jacksonville, TX 75074| ",
"THS& | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|"
]
*/
答案 3 :(得分:1)
尝试一下
//create adapter class implementing the on click listener
public class WordListAdapter extends RecyclerView.Adapter<WordListAdapter.WordViewHolder> implements View.OnClickListener {
private final LinkedList<String> mWordList;
private LayoutInflater mInFlater;
private final RecyclerView recyclerView;
//in constructor, pass in RecyclerView created in MainActivity
public WordListAdapter(Context context, LinkedList<String> wordList, RecyclerView rView) {
recyclerView = rView; <----
mInFlater = LayoutInflater.from(context);
this.mWordList = wordList;
}
//implementation of View.OnClickListener makes
@Override
public void onClick(View view) {
//get the position of the item that was clicked
int mPosition = recyclerView.getChildAdapterPosition(view); <----
//other code to do what you want with the list(eg. String element = mWordList.get(mPosition);)
}
//And you set your pointer in the adapter class
@Override
public WordListAdapter.WordViewHolder onCreateViewHolder( @NonNull ViewGroup parent, int viewType) {
View mItemView = mInFlater.inflate(R.layout.wordlist_item, parent, false);
mItemView.setOnClickListener(this); <----
return new WordViewHolder(mItemView, this);
}
THS之前的空格用于修剪最后一个空格