Check for String
posted 21 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi All,
The code:
String clfiles= "9,8,7,6,5,19,18,17,16,15,14,13,12,11,10";
String fileno="1";
In the above part of code,clfiles might grow according to the case.I am checking for the particular exact number in the clfiles
ie checking for exact fileno in clfiles.
substrings usually bring the first occurences.
Please help me out.
I am stuck.
Thanks,
Bhuvana
The code:
String clfiles= "9,8,7,6,5,19,18,17,16,15,14,13,12,11,10";
String fileno="1";
In the above part of code,clfiles might grow according to the case.I am checking for the particular exact number in the clfiles
ie checking for exact fileno in clfiles.
substrings usually bring the first occurences.
Please help me out.
I am stuck.
Thanks,
Bhuvana
posted 21 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
... or better yet, a regular expression that would look for the particulars of your interest...
posted 21 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Bhuvana,
This sounds like maybe a programming assignment, so i will give you a couple ideas but won't give you the code.
You are on the right track using substring. if you know that a comma is at position p in String s, then you can use s.substring(p+1) to give you the part of the String that is to the right of the comma.
You can use the indexOf method to find out the position of the next comma.
Combine these in a loop and you have a powerful way to loop through the string.
You can then do a couple of things: You can "tokenize" the string as mentioned by Ashok. That means creating an array (or List) and putting the separate elements in the array and then you can loop through it easily.
Or if you are simply looking for an item (such as the first occurance of the number "17" then you can stop when (if) you find it.
It can be confusing at first in figuring out how to put the loop together. What i suggest is that you write down, in words, the behavior of the program/loop. In other words, how would YOU find the first occurance of the number "17" in such a list? What do your eyes and mind do as you are looking for it? you start at the beginning, look for a 17 that is between 2 commas, etc.
Good luck,
Tony
This sounds like maybe a programming assignment, so i will give you a couple ideas but won't give you the code.
You are on the right track using substring. if you know that a comma is at position p in String s, then you can use s.substring(p+1) to give you the part of the String that is to the right of the comma.
You can use the indexOf method to find out the position of the next comma.
Combine these in a loop and you have a powerful way to loop through the string.
You can then do a couple of things: You can "tokenize" the string as mentioned by Ashok. That means creating an array (or List) and putting the separate elements in the array and then you can loop through it easily.
Or if you are simply looking for an item (such as the first occurance of the number "17" then you can stop when (if) you find it.
It can be confusing at first in figuring out how to put the loop together. What i suggest is that you write down, in words, the behavior of the program/loop. In other words, how would YOU find the first occurance of the number "17" in such a list? What do your eyes and mind do as you are looking for it? you start at the beginning, look for a 17 that is between 2 commas, etc.
Good luck,
Tony
| You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









