String& char
Hi;
Could anyone tell me a buildin class for find out how many characters in a text file(or a string) and it's length(count by character).
For example;
"ghggjjkk kjkjk"
it has 14 characters and length is 14
Thanks
For Strings, there's the length() method. The length of a String is the number of characters it contains.
I think the java.io.File class has a size() or length() method that will tell you how many bytes are in the file. Look at the API docs for details. To convert that to a number of characters, you'd pretty much have to read it in with an appropriate encoding. And count the number of characters as you read it.
Similar Messages
-
How to to scan a string, char by char?
Hi i have some xml data, i want to scan the string char until it sees > for the first time. Then it adds the following data until it sees <, at which point it stops. So i should get s2tidm1 from "<server>s2tidm1</server>"
The problem with the below code is that sa.next() gives me the whole string as there is no space, is there a sa.nextChar() or something similar?
Code following -- Thanks Peter
import java.io.*;
import java.util.Scanner;
import java.util.*;
public class xmltestmod {
static String data = "<server>s2tidm1</server>";
public static void main(String args[]) {
Scanner sa = new Scanner(data);
String pete = " ";
String temp =" ";
while (sa.hasNext()) {
temp = sa.next();
System.out.println(temp);
if (temp.contains(">")){
while (true){
System.out.println("Entering if");
temp = sa.next();
pete = pete + temp;
if(temp.contains("<")) break;
System.out.println(pete);
sa.close();
}>
I got muiltiple xml lines in a array, i want to loop
the array and strip out the tags so only the data is
left. Thats why i didn't use the xml apis out there
as you need to say starttag = "<Server>";, endtag =
"/Server>", but i just want to say store all data
inbetween < and >, that way i can loop through the
whole array.
Actually if you use the SAX parser, rather than the DOM parser, it will call a method of your chosing for each text element, and you can just ignore the callbacks for tags etc. if you wish.
Can you explain abit more about hte substring and
index of part please.Check the Javadocs. Substring chops part of a string out, indexOf finds the first occurance of a character. -
Hi,
I need to know if it possible, and how to send a string message to the printer and control the print from the keyboard.
I will explain:
A known text should be print by the printer in a way that each key press (spacebar or other key) will command the printer to print the next char.
i.e the text is "Hello World", the first space bar hit will result "H" on the printer paper, the second spacebar hit will result "e" on the printer paper and "He" is the the current string printed... etc.
if you think its possible or if you know how to do it, I will be happy if you share it with me.
Thanks.If you're interested in this sort of thing, the following may be of help:
A Demonstration: Using the Java� Print Service API
Printing in Java, Part 1: Acquaint yourself with the Java printing model
The Java� Tutorial - I/O: Reading and Writing (but no 'rithmetic) -
Is there an equivalent to the "!" operator for strings and characters, to put it in context, how would I write the piece of code between the parenthesis correctly?
while("Y" != answer)
number=1;
}You can write
while(!"Y".equals(answer) { //"Y" does not equal "answer"
//code here
}Of course, answer has to be of type string.
You can also do the same with chars. Same code.
edit
Link for char. I assume it compares the value's of the chars and determines if they are equal from there:
http://java.sun.com/j2se/1.3/docs/api/java/lang/Character.html#equals(java.lang.Object)
Message was edited by:
lethalwire -
Is there a String(char, ntimes) function ?
Hello
I'm looking for a simple function that creates a string with some char , n times.
In the old VBasic it can be possible with: a=String("h",4) >>> hhhh
Is there something similar in Java ?
Thankssabre150 wrote:
tonnot wrote:
( If you want .... what number of lines of code are need to construct a string with 15 characters - obviously not always the same )
String s1 = new String(Arrays.copyOf(new char[]{}, 15)).replace('\0', '4');Just replace the '15' with the length and the '4' with the desired character.Why not
String s1 = new String(new char[15]).replace('\0', '4'); -
Quick String / char question
Hi, i have to write a encrytion method for a assignment, it must take a string, move the characters on by a set number given by the user and return the result.
public class Encryption
int key;
public Encryption(int k)
key = k;
public String encrypt(String si)
si = si.toUpperCase();
int i = 0;
for (int index = 0; index< si.length(); index++);
char c = si.charAt(i);
int n = c - 'A' + key;
char a = (char)(n + 'A');
return si ;
}it compiles, but what i need to know is how to i get my new chars (that have been moved the set amout) back into a string that is returned to the user, as i cant seem to find the code i need to do this???String result = "";
Each time through the loop, you will want to add it to the result
result = result + a;This is poor adviceimport java.io.*;
public class Encryption{
public static void main(String[] args) {
new Encryption();
Encryption(){
String toEncyrpt = "The quick brown fox jumps over the lazy dog";
int key = 0;
try{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number for the encryption key ");
key = Integer.parseInt(br.readLine());
}catch(IOException e){}
String encrypted = encrypt(toEncyrpt, key);
System.out.println("Encrypted string : "+ encrypted);
String decrypted = decrypt(encrypted, key);
System.out.println("Decrypted string : "+ decrypted);
public String encrypt(String s, int k){
StringBuffer sb = new StringBuffer();
for(int i=0; i<s.length(); i++) {
int temp = (int) s.charAt(i)+k;
sb.append((char)temp);
return sb.toString();
public String decrypt(String s, int k){
StringBuffer sb = new StringBuffer();
for(int i=0; i<s.length(); i++) {
int temp = (int) s.charAt(i)-k;
sb.append((char)temp);
return sb.toString();
} -
Method(String, char) not applicable for the arguments()
Hi. Im trying to run this method that I made but I cant coz I always get this error. the ff is my code:
class Sample {
private String word = "apple";
private char a = 'p';
public static void main(String[] args)
callingCounter();
public int counter(String word, char a)
int position = 0;
for (int i=0; i<word.length(); i++)
position = word.indexOf(a);
System.out.println(position);
return position;
public int numberOfLetters(String word, char a)
return numberOfLetters;
thanks a lotand your method should be
public int callingCounter(String word, char a)
int position = 0;
for (int i=0; i<word.length(); i++)
position = word.indexOf(a);
System.out.println(position);
return position;
} -
Convert char[] to string
Hi,
How can I convert a char array to string?Don't worry about it.
Just I have to use String(char[])
Thanks :) -
Hi, I'm trying to use a .DLL I wrote in Visual C++ .NET. The Call Library Function generates a function prototype of "CStr Parser(CStr arg_raw)"
but my compiler won't accept this. So I change it to how C normally deals with strings "char *Parser(char *arg_raw). Now the Call Librafy Function does not find "Parser" in my .DLL though it is definitely there!
Help!
-FongHello
You will need to include extcode.h in your C file. You can find this under ..\LabVIEW\cintools folder.
The include has all the defines you will need.
Bilal Durrani
NI
Bilal Durrani
NI -
Converting more than one char back to a string.
I know this:
String s = Character.toString(letter);
will convert one char back to a string.
What about if you have more than one char, how do you convert them back to a string???When you have more than one char, you probably have them in an array. In that case you can use the String(char[]) constructor.char[] hello = new char[5];
hello[0] = 'h';
hello[1] = 'e';
hello[2] = 'l';
hello[3] = 'l';
hello[4] = 'o';
String salute = new String(hello); -
Converting an array of char to a String object
let's say i have an extremely long character array of 1 million chars and store a text file to it. is it safe to cast the character array into a String object? if not, what is a safe way to convert it to a String? I need to search through the String and create substrings and stuff. Thanks you!
HOMEWORK ALERT HOMEWORK ALERT
You can't cast from a character to a String. That doesn't work.
However, in a fit of being helpful.
Look at the constructors for the String object in the API
Link ===> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#String(char[])
char[] charA = {'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D' } ;
String a = new String(charA) ;
System.out.println(a) ;There's a price for my help, and it's this. Now that you understand what you can do, is it a good idea or a bad idea and why ???
Explain. -
Making one big String with the same char
I need to generate a String which represents only one char n-times.
Something like:
String str = new String(char Source, int iRepeat)
String str = new String('x', 5);
str = 'xxxxx';
Thanks in advance!public String createString(char c, int n) {
StringBuffer sb = new StringBuffer();
for ( int y=0; y <= n; y++ ) {
sb.append(c);
return sb.toString();
}that should work.
always use string buffers when youre appending text. when you change the value of a STRING object, the jvm actually creates a new string in memory, and just changes the reference to the new string (since each variable you declare as STRING is actually just a reference). this isnt a problem with small strings, but if you create a string thats fairly large, youll notice a huge performance difference. so, just use Strings for storing values, and StringBuffers for string values you plan on changing often. -
Generate unique 10 char string
Hi,
i have to generate a unique 10 char string ( a string with length of 10 ) which has numbers and alphabets with no special characters, is there any code of doing it
Ashishclass UniqueString {
private static final int NUM_CHARS = 10;
private static int[] lastString = new int[NUM_CHARS];
private static final String chars =
"0123456789abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String getUnique() {
char[] buf = new char[NUM_CHARS];
carry(lastString, buf.length-1);
for(int i=0;i<buf.length;i++) {
buf[i] = chars.charAt(lastString);
return new String(buf);
private static void carry(int[] ca, int index) {
if(ca[index]==(chars.length()-1)) {
ca[index]=0;
carry(ca,--index);
else ca[index]= ca[index]+1;
public static void main(String[] args) {
for(int i=0;i<10000;i++)
System.out.println(UniqueString.getUnique());
Guarenteed to be unique within one VM instance.
matfud -
Hi-ya all
Two questions, both performance related.
1. What would be the fastest way to produce a one char string, is there anything faster than new String(char + "").
2. What would be the fastest way to get an uppercase from a char, is there anything better than a switch and or set of if/elses, for example some sort of fiddle factor?
thanksCharacter.toString(char)
Character.toUpperCase(char) -
How can I get the first character from a string? I was given the code below before but I do not think that it works correctly. If it does work then can more people please look at my previous thread and give me a hand.
char firstNumber = inputString.charAt(0); //first character of string
char secondNumber = intputString.charAt(InputString.length()-1); //last character of stringAs always, any help is greatly appreciatedApart from the obvious typos, it looks just fine to me. Why don't you think it works?
Maybe you are looking for
-
How to display updated values in an array of clusters?
Hi all! I am designing and coding a LabView application. In one of the modal dialog boxes of the application, I have to dynamically create an array of a cluster in the front panel. The array of the cluster is one dimensional. The number of rows of th
-
Getting the number of pages in excel sheet or current page using OLE2
Hi, I fill an excel sheet using CLIENT_OLE2 from Oracle 10g form, I want to get the number of pages in sheet or the number of current page, after searching for solution I found this example on VB: Sub GetPageCount() Dim iView As Integer Dim iHorizont
-
Login screen alterations.
I recently found out you can create custom login screen pictures and remove the apple logo and so on. A part I also loved was the login message. I was wondering if anyone knew how to alter it so it could be made larger, text change and have new line
-
When I boot iCal, I get the whirrling dervish and the application doesn't fully open. I've had to force quit each time. I've removed the iCal preferences from the Library/Preference folder. Do I need a new 10.4.6?
-
Facebook contacts only partially syncing.
I addded my facebook account in system preferences in hope of syncing my contacs and their birthdays but it seem that OS X is unable to sync all contacts. I have on 80 or so contacts of my 400+ friends synced with OS X. Is it a known problem? How ca