String and whitespace problem

S

Sharp

Hi

I have a sting that contains long stretches of whitespaces between
characters.

For example:

String str = " A B C
";

I would like to include all the characters including the white spaces into a
data structure (e.g., ArrayList).
The code I have written is something like this:

List list = new ArrayList();
for (int i=0; i<str.length(); i++)
{
list.add(str.charAt(i));
}

Unfortunately it just adds "ABC" into the array list.
It seems charAt() method ignores whitespaces,
But the documentation doesn't mention anything about whitespaces.
Iam happy to add another character in place of the whitespace,
So I tried using the replaceAll() method from the string class, but that
didn't work.

Any advice will be appreciated.

Cheers
Sharp
 
S

Sharp

Hi

I have a sting that contains long stretches of whitespaces between
characters.

For example:

String str = " A B C
";

I would like to include all the characters including the white spaces into a
data structure (e.g., ArrayList).
The code I have written is something like this:

List list = new ArrayList();
for (int i=0; i<str.length(); i++)
{
list.add(str.charAt(i));
}

Unfortunately it just adds "ABC" into the array list.
It seems charAt() method ignores whitespaces,
But the documentation doesn't mention anything about whitespaces.
Iam happy to add another character in place of the whitespace,
So I tried using the replaceAll() method from the string class, but that
didn't work.

Any advice will be appreciated.

Cheers
Sharp

I forgot to mention that Iam wrapping the characters using the Character
class before adding it into the array.

For example:

Character ch = new Character(str.charAt(i));
list.add(ch);

Again any help will be appreciated.

Cheers
Sharp
 
R

Roland

I forgot to mention that Iam wrapping the characters using the Character
class before adding it into the array.

For example:

Character ch = new Character(str.charAt(i));
list.add(ch);

Again any help will be appreciated.

Cheers
Sharp
String.charAt(int) does not ignore white space. There must be something
else going on, maybe the string you process already has been stripped of
whitespace.

The following program should convince you that charAt is not discarding
any characters of the original string.

import java.util.ArrayList;
import java.util.List;

public class WhiteSpace {
public static void main(String[] args) {
String str = " A B C \n ";
List list = new ArrayList();
for (int i = 0; i < str.length(); i++) {
list.add(new Character(str.charAt(i)));
}
System.out.println(str.length());
System.out.println(list.size());
}
}
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
S

Sharp

into

a



I forgot to mention that Iam wrapping the characters using the Character
class before adding it into the array.

For example:

Character ch = new Character(str.charAt(i));
list.add(ch);

Again any help will be appreciated.

Cheers
Sharp
String.charAt(int) does not ignore white space. There must be something
else going on, maybe the string you process already has been stripped of
whitespace.

The following program should convince you that charAt is not discarding
any characters of the original string.

import java.util.ArrayList;
import java.util.List;

public class WhiteSpace {
public static void main(String[] args) {
String str = " A B C \n ";
List list = new ArrayList();
for (int i = 0; i < str.length(); i++) {
list.add(new Character(str.charAt(i)));
}
System.out.println(str.length());
System.out.println(list.size());
}
}
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \

I agree that something strange is happening.

When I open the text file containing the strings that I am reading in using
FileReader wrapped in BufferedReader,
There is clearly whitepaces between characters.
But it is only reading the characters into the array and ignoring the
whitespaces.

I need to doublecheck it tomorrow with a fine comb.

Thanks

Sharp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top