从String中提取特定部分

时间:2017-07-07 19:32:54

标签: java

我有以下package com.rxmedicalapp.customviews; public class TextView extends AppCompatTextView { public TextView(Context context) { super(context); init(null); } public TextView(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public TextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } private void init(AttributeSet attrs) { if (attrs != null) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextView); int fontCode = a.getInt(R.styleable.TextView_font,Integer.MAX_VALUE); Typeface myTypeface=null; switch (fontCode){ case 0: myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_light.ttf"); break; case 1: myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_regular.ttf"); break; case 2: myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_bold.ttf"); break; case 3: myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/robotolight.ttf"); break; case 4: myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/robotoregular.ttf"); break; default: myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/robotoregular.ttf"); break; } setTypeface(myTypeface); a.recycle(); } } public void setUnderLine(boolean underlined){ } 变量

String

我想只提取字符串(String s = "abc,xyz,lmn,ijk";

的部分内容

并且,不应使用内置函数,例如i.e - 'lmn'但我可以使用SubString(), Split(), IndexOf().

我的采访中提到了这个问题。

我尝试了以下代码, 但不知道如何继续。任何人都可以提出你的想法吗?

charArray()

3 个答案:

答案 0 :(得分:1)

这是一种方式:

public static void main(String[] args) {
    String s = "abc,xyz,lmn,ijk";
    char[] ch = s.toCharArray();
    int counter = 0;
    int place = 2;

    for (int i = 0; i < ch.length-2; i++) {
        if(ch[i] == ',') {
            counter++;
        }
        if(counter == place && ch[i] != ',') {
            System.out.print(ch[i]);
        }
    }
}

它会在第二个逗号之后但在第三个逗号之前打印所有内容。

答案 1 :(得分:1)

function csvFunc(params) {
    return new Promise(function(resolve, reject) {
    //do something CSV
    });
}

function boxManager(params) {
    return new Promise(function(resolve, reject) {
    //do something box, create box
    });
}

Promise.all([csvFunc(params), boxManager(params)]) //add the functions what you want,
    .then(function(allData) { //after all functions are Ok, then....
        // return the value for slack or what you want to do
        console.log("All functions have perfomed")
    });

答案 2 :(得分:1)

我会这样做。

String s = "abc,xyz,lmn,ijk"; 
String x = "c,x"; // String to found
String r = "";

boolean coincidence = false;

int a=0; // Initial index if of the first character in x is found
int b=0; // Last index If it was possible to search for the last character of x
int c=0; // Index "iterator" on String x

char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
  if(c == x.length())
    break;
  else{
    if(ch[i] == x.charAt(c) && !coincidence){
      a = i; b = i; c++;
      coincidence = true;
    }
    else if(ch[i] == x.charAt(c) && coincidence){
      b++; c++;
    }else{
      coincidence = false;
      a = 0; b = 0; c = 0;
    }
  }
}

System.out.println("String: " + s);
System.out.println("String to find: " + x);
System.out.println("Was found? " + ((coincidence)? "Yes" : "No"));

if(coincidence){
  System.out.println("Intervals indexes in String: ["+a + "," + b +"]");
  // String extration
  for (int i = a; i <= b; i++) 
    r += s.charAt(i);

  System.out.println("String extracted: " + r);
}