Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using methods on every item in an Array

Tags:

java

I have a string array that is being filled dynamically.... I would like to run this function:

String[] name = request.getParameterValues("name");
myString = name.substring(0, name.length() - 2);  

Now, I know that this wont run, because substring and length methods aren't used for the whole array. Since i dont know how many items will be in this array at any given run time, is there anyway to run the substring/length function for every item in this array?

like image 236
Dan Avatar asked Sep 14 '26 22:09

Dan


1 Answers

Yes, use you can use length field of the name array. Like this:

for (int i = 0; i < name.length; ++i)
{
    String s = name[i];
    String sbtr = s.substring(0, s.length() - 2);
}

Or a better approach would be this:

for (String s: name)
{
    String sbtr = s.substring(0, s.length() - 2);
}
like image 73
insumity Avatar answered Sep 16 '26 10:09

insumity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!