find the position of the field separator in the String---need help ASAP
Hi All,
I have the following input with the Pipe Field Separator(i.e Each filed is separated by Pipe).
My requirement is
i need to take the string required and do some validations
how do i get the filed with the name required from the following string.
java|21|new1|kkkk|eee|rrrr||dddd|dddd|ooooo|2073|| ||||7747474|48888|required||||444444|.
java|22|new2|kkkk|eee|rrrr||dddd|dddd|ooooo|2073|g gggg||rrrrrrrr||5555555|7747474|48888|required|||| 444444|.
The record separator is NewLine and the length of each record is different.
Is there any method() to find the position of the field separator pipe?
Re: find the position of the field separator in the String---need help ASAP
say
String str = "java|21|new1|kkkk|eee|rrrr||dddd|dddd|ooooo|2073| | ||||7747474|48888|required||||444444|";
int index = str.indexOf("|");
this gives you the index of pipe in the string. But given that you have lot of pipes, use one of the following overloaded methods of indexOf and apply some logic:
int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
int indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
I would use last method and keep changing the second parameter to get every position of pipe.
Acumen,
Lucid forums
Re: find the position of the field separator in the String---need help ASAP
How about converting it to an array, that way you could loop through it.
Code :
final String inputString = "java|21|new1|kkkk|eee|rrrr||dddd|dddd|ooooo|2073| | ||||7747474|48888|required||||444444|";
final String[] array = inputString.split("\\|");
// Json
Re: find the position of the field separator in the String---need help ASAP
Are you looking to extract the word 'required' and print the position of it within the String?
Re: find the position of the field separator in the String---need help ASAP
yes Exactly(Is there any methods to find out the postion of the string without splitting ).if you know any solution just give me reply
Re: find the position of the field separator in the String---need help ASAP
Quote:
String index = str.indexOf("required");
that is what you want right?
Acumen,
Lucid forums
Re: find the position of the field separator in the String---need help ASAP
Quote:
Originally Posted by
rajesh.mv
yes Exactly(Is there any methods to find out the postion of the string without splitting ).if you know any solution just give me reply
You mean like this then? For the purpose of this code the Strings are in a file called pipefile.txt
Code :
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Pipe {
/**
* JavaProgrammingForums.com
*/
public static int position;
public void readFile(){
try
{
FileInputStream in = new FileInputStream("pipefile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while((strLine = br.readLine())!= null)
{
System.out.println(strLine);
position = strLine.indexOf("required");
System.out.println("'required' at position " + position);
}
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args) {
Pipe p = new Pipe();
p.readFile();
}
}
Example output:
Quote:
java|21|new1|kkkk|eee|rrrr||dddd|dddd|ooooo|2073|| ||||7747474|48888|required||||444444|.
'required' at position 69
java|22|new2|kkkk|eee|rrrr||dddd|dddd|ooooo|2073|g gggg||rrrrrrrr||5555555|7747474|48888|required|||| 444444|.
'required' at position 89