Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

How do I read only a specific part of the string in Android Studio?

If a send a string in the following format: 1234,1234,1234,1234; from Arduino to Android Studio (java (intelliJ)) based. with the amount of characters between every komma changing. How do i make it so that my code only reads the string from for example 0 to the , Or from the first , to the second ,?

question from:https://stackoverflow.com/questions/65888884/how-do-i-read-only-a-specific-part-of-the-string-in-android-studio

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can split the input string based on any special character,in your case ,, as follows

String inputString = "1234,1234,1234,1234";
String[] separated = inputString.split(",");

Log.i("MainActivity",separated[0]) // prints the first string which is 1234

// to loop over all strings
for(String s : separated){
     Log.i("MainActivity",s)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...