Java – use methods on each item in the array
•
Java
I have a dynamically populated string array I want to run this function:
String[] name = request.getParameterValues("name"); myString = name.substring(0,name.length() - 2);
Now, I know this won't work because the substring and length methods are not used for the entire array Since I don't know how many items there are in this array at any given runtime, should I run the substring / length function for each item in this array anyway?
Solution
Yes, you can use the length field of the name array
for (int i = 0; i < name.length; ++i) { String s = name[i]; String sbtr = s.substring(0,s.length() - 2); }
Or better:
for (String s: name) { String sbtr = s.substring(0,s.length() - 2); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码