请帮我分一下这样的字符串" mumbai(或)pune"使用java。
我希望")"之后的字符串,我尝试使用String result="pune".
但不使用上面的字符串格式。
我的预期输出是" pune"。
输入:
public class MyCanvas extends View {
private int total;
private int width;
private int height;
@Override
public void onDraw(Canvas canvas){
// Drawing my stuff here with (width and height gives me 0 here)
if(total == 50 || total == 150){ total == 100;}
}
public int addToCanvas(int value){ total += value ;}
public int subtractToCanvas(int value){ total -= value ;}
//All three of contructors here
public MyCanvas(Context context) {
super(context);
total = 100;
width = getWidth();
height = getHeight();
}
public MyCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
total = 100;
width = getWidth();
height = getHeight();
}
public MyCanvas(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
total = 100;
width = getWidth();
height = getHeight();
}
}
输出:
public class MainActivity extends Activity {
private MyCanvas mycanvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mycanvas= (MyCanvas) findViewById(R.id.customcanvas);
mycanvas.addToCanvas(10);
}
}
答案 0 :(得分:1)
如果您的输入字符串始终与您显示的字符串相似:
yourString.split("\\)")[1].trim();
答案 1 :(得分:1)
您可以将其拆分为多个部分:
String abc="mumbai (or) pune"
String result = "pune";
String[] parts = abc.split(" ");
String partOne = parts[0];
String partTwo = parts[2];
if (partOne == result){
System.out.println(partOne);
}
else{
System.out.println(partTwo);
}
答案 2 :(得分:0)
试试这个:
String s = "mumbai (or) pune";
String result = s.substring(s.lastIndexOf(')') + 1).trim();
答案 3 :(得分:0)
替换在这里实际上会更有效率。
使用:
String abc="mumbai (or) pune";
abc = abc.replaceAll(".*\\s+(\\w+)","$1");
// abc will be "pune" here
答案 4 :(得分:0)
有很多方法可以做你想要的简单事情。最佳方法:阅读Java中的字符串操作
以下是实现您所需要的一些方法:
public static void main(String[] args)
{
String output1 = "mumbai (or) pune";
String output2 = output1.split("or\\) ")[1];
String output3 = output1.substring(output1.indexOf(")")+2);;
System.out.println(output1);
System.out.println(output2);
System.out.println(output3);
}