我正在一个项目上,需要设置以0、1或2开头的旧数据以简单地等于一个级别。
if (dr["Acct"].ToString().Substring(0,1) == "0"
|| dr["Acct"].ToString().Substring(0,1) == "1"
|| dr["Acct"].ToString().Substring(0,1) == "2")
{
// then Level = Balance Sheet
}
我实质上是想说子字符串是0、1还是2,那么它应该等于称为资产负债表的水平。
答案 0 :(得分:2)
如果您要检查多个字符串,我会将其存储在字符串数组中并检查其可读性
public class TeachersAdapter extends RecyclerView.Adapter<TeachersAdapter.TeacherAdapterViewHolder> {
private ProgressDialog LoadingBar;
FirebaseAuth teacherAuth;
DatabaseReference teacherRef;
FirebaseDatabase db;
public Context c;
public ArrayList<Modal_Teachers> arrayList;
public TeachersAdapter(Context c, ArrayList<Modal_Teachers> arrayList){
this.c= c;
this.arrayList = arrayList;
}
@Override
public int getItemCount() {
return arrayList.size();
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public TeacherAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_teachers,parent, false);
Button edit_t = v.findViewById(R.id.teacher_edit);
Button delete_t = v.findViewById(R.id.teacher_edit);
return new TeacherAdapterViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final TeacherAdapterViewHolder holder, final int position) {
final Modal_Teachers sTeachers = arrayList.get(position);
holder.teacherName.setText(sTeachers.getName());
holder.teacherDesignation.setText(sTeachers.getQualification());
Picasso.get().load(sTeachers.getPhotourl()).into(holder.teacherImage);
public class TeacherAdapterViewHolder extends RecyclerView.ViewHolder{
public CircleImageView teacherImage;
public TextView teacherName,
teacherDesignation;
public Button edit_t, delete_t;
public TeacherAdapterViewHolder(@NonNull View itemView) {
super(itemView);
teacherImage = itemView.findViewById(R.id.image_teacher);
teacherName = itemView.findViewById(R.id.teacher_nome);
teacherDesignation = itemView.findViewById(R.id.teacher_designation);
edit_t = itemView.findViewById(R.id.teacher_edit);
delete_t = itemView.findViewById(R.id.teacher_delete);
}
}
}
答案 1 :(得分:2)
字符串操作非常昂贵,这可能是一种更具可读性的处理方式。
string acctVal = dr["Acct"]?.ToString();
char[] balanceSheetVals= {'0', '1', '2'};
if(!String.IsNullOrEmpty(acctVal))
{
if(balanceSheetVals.contains(acctVal[0]))
{
Level = "Balance Sheet";
}
else
{
//defaults
}
}
else
{
//defaults
}
编辑:@Rotem的大多数道具都清理了很多
答案 2 :(得分:2)
如果您要检查某个string
是否以另一些string
开头,我个人认为String.StartsWith
比使用{{ 1}}。
String.Substring