Results 1 to 9 of 9
- 12-30-2009, 01:05 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
[SOLVED]Java split string, the first char is a space
Hi guys,
I'm doing 2 splits from a txt file, the 1st split saves the 1st char of the file
the next split saves the rest of the line.
For example the line is:
"Ab Just an example"
The first split saves.me the word "Ab" but the other split saves the phrase:
" Just an example" the thing is that I get an empty space in the begining of the phrase, what I need to change?
The code that I'm using is:
Thanks and sorry my poor english.firstChar=txtFile.next().split(" ");
theRestPhrase=txtFile.nextLine().split("\n");
RegardsLast edited by new_cyber; 12-30-2009 at 03:26 PM. Reason: Question solved
- 12-30-2009, 01:22 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I think you mean you are trying to get the first word and then the rest of the line.
split() actually returns an array of strings so it's a bit hard to understand the code you posted. Perhaps you could post a small compilable example that is giving the undesirable output?
- 12-30-2009, 01:29 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Yes, that What I want, to get the firsd word and then the rest of the line.
I'm using a file reader, the thing is , I'm reading a .txt file, the first splitsaves the frist word and I do other split to save the rest of the linefirstChar=txtFile.next().split(" ");
and it works, but the begining of the rest of the line is an empty space and I want to remove the empty space but I dont know how.theRestPhrase=txtFile.nextLine().split("\n");
- 12-30-2009, 03:02 PM #4gcampton Guest
ok do this:
outputJava Code:String myString = "Ab Just an example"; String firstWord, restOfString=""; String[] aSplit = myString.split(" "); firstWord=aSplit[0]; // now for the rest of string we can just append the rest for (int i=1; i<aSplit.length; i++) { restOfString += aSplit[i]; restOfString += " "; } System.out.println("First word = [" + firstWord + "]"); System.out.println("Rest of string = [" + restOfString + "]");
Java Code:First word = [Ab] Rest of string = [Just an Example]
Last edited by gcampton; 12-30-2009 at 03:24 PM.
- 12-30-2009, 03:25 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
- 12-30-2009, 03:29 PM #6gcampton Guest
Do you understand how to use .split() from my example now?
it's a lot easier to make arrays and use arrays on the string.split() then trying to use scanner and split combined.
if you want to use scanner then use the scanner.Delimiter(" "); method, it will do your way of trying to break it up. (.next() & .nextLine())
Just look up the Scanner API
- 12-30-2009, 07:04 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
- 12-30-2009, 07:40 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
A more accurate approach would be to avoid arrays and use String methods: indexOf() will find the first space and the two variants of substring() will enable the word and the rest of the line to be extracted. trim() might also be useful when dealing with the second part.
- 12-31-2009, 01:27 AM #9gcampton Guest
And another way of doing it.
Java Code:import java.util.Scanner; public class stringTest { public static void main(String[] args) { String abc = "Abc Rest of String goes here"; String firstWord, restOfString; Scanner sc = new Scanner(abc); firstWord = sc.next(); restOfString = sc.nextLine(); System.out.println(firstWord); System.out.println(restOfString); restOfString = restOfString.substring(1,restOfString.length()); // restOfString = restOfString.trim(); = less typing :D System.out.println(restOfString); } }Last edited by gcampton; 12-31-2009 at 01:31 AM.
Similar Threads
-
string split
By gisler in forum New To JavaReplies: 6Last Post: 12-17-2009, 02:23 PM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
JAVA: String char removal with nested loop
By igniteflow in forum New To JavaReplies: 3Last Post: 11-28-2008, 02:09 AM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM -
Char to String in java
By trill in forum New To JavaReplies: 1Last Post: 08-01-2007, 01:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks