Adding Characters to a string in a loop
Hi
I have a task where if the text perameter is more than 10 characters in cuts it down to 10, which i can do. If it is 10 it just gives it back, which i can do. But if it is less than 10 you need to add extra spaces onto the end of it. I cant seem to think how to do it. What i have at the moment seems to put it in an infinite loop. Any tips or hints would be much apreciated.
public class Main
public static String trimTo10(String s)
int i, len = s.length();
String results = "";
if (len >10)
for (i =0; i <= 9; i++) {
char w=s.charAt(i);
results = results + w ;
if (len ==10){
results = s ;
if (len <10)
for (i =0; len<=9; i++) {
results = s + " ";
return results; }
}
Hi
You need to decide on exactly what you want to do. Write an algorithm of the process. (it doesn't have to be formal)
Something like:
<length is less than 10>
get length of string
number of spaces = 10 - length of string
That should be enough to get you started...
regards,
lutha
Similar Messages
-
hi guys,
i have to do this:
i take the string and read it character by character and everytime i read a new character, i put it into my output string until i reach the \n character, there i simply return my output string.
THE problem: i need to be able to read it characters from my input string.
I can't use a stringbuffer because that brings up other complications later inmy program.
I basically need to know can i read something character by character and put it into a string
thanx!
public String Split(String input, int i)
char carriagereturn= '\n';
String output="";
try
while(i<input.length())
if(input.charAt(i)== carriagereturn )
return output;
if(input.charAt(i)!= carriagereturn)
output.concat(input.charAt(i));
i++;
} catch (Exception e)
return output;
}//end of method Splitimport java.io.*;
public String split(String s) {
ByteArrayInputStream bstream = new ByteArrayInputStream(s.getBytes());
StringBuffer buf = new StringBuffer();
int c;
int CR = 13;
int LF = 10;
while ((c = bstream.read()) != -1) {
if (c == CR || c == LF)
break;
buf.append((char)c);
return buf.toString(); -
Removing non-alpha-numeric characters from a string
How can I remove all non-alpha-numeric characters from a string? (i.e. only alpha-numerics should remain in the string).
Or even without a loop ?
Extract from the help for the Search and Replace String function :
Right-click the Search and Replace String function and select Regular Expression from the shortcut menu to configure the function for advanced regular expression searches and partial match substitution in the replacement string.
Extract from the for the advanced search options :
[a-zA-Z0-9] matches any lowercase or uppercase letter or any digit. You also can use a character class to match any character not in a given set by adding a caret (^) to the beginning of the class. For example [^a-zA-Z0-9] matches any character that is not a lowercase or uppercase letter and also not a digit.
Message Edité par JB le 05-06-2008 01:49 PM
Attachments:
Example_VI_BD4.png 2 KB -
Unable to read more than 255 characters in a string data type.
Hello,
I am using Crystal Reports version 11.5.8.826 to generate reports from databases such as MS-Excel & BaaN.
In Excel, there are records which carry string lengths of more than 255 characters. All though the fields are getting displayed in the report, the data being displayed gets truncated once it reaches a length of 255 characters.
Kindly provide me with a solution this problem.
RegardsHello Manish,
I know that older versions of Crystal (Crystal 10 and older ) had a limitation of only 256 Characters for a string value. You should be able to use this code to get what you need.
We were successfully able to print out the Declaration of Independence through a Crystal report using something very similiar to this.
u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2013
Shared StringVar Array strings;
Shared NumberVar arrayMax := 1;
Shared NumberVar maxLength := 250;
Shared BooleanVar firstTime := true;
Redim strings[arrayMax];
strings[1] := u201Cu201D;
u2018done!u2019
u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2013
Here is my main loop that I used in the group header:
u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2013
WhilePrintingRecords;
Shared StringVar Array strings;
Shared NumberVar arrayMax;
Shared NumberVar maxLength;
Shared BooleanVar firstTime;
Local StringVar enteredString;
Local NumberVar lenStr;
Local NumberVar lenArr;
Local NumberVar lenLeft;
enteredString := [Field Name];
enteredString := IIF(firstTime,enteredString, u201C, u201D & enteredString);
firstTime := false;
lenStr := Length(enteredString);
lenArr := Length(strings[arrayMax]);
lenLeft := maxLength u2013 lenArr;
if (lenLeft >= lenStr) then
strings[arrayMax] := strings[arrayMax] & enteredString;
u201D
else
strings[arrayMax] := strings[arrayMax] & left(enteredString, lenLeft);
arrayMax := arrayMax + 1;
redim preserve strings[arrayMax];
strings[arrayMax] := u201Cu201D;
strings[arrayMax] := strings[arrayMax] & right(enteredString, lenStr u2013 lenLeft);
u201D
u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2013
and then to display the different sets in the text area. I would have one of these for every element I need displayed and change the display number variable.
u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2013
Shared StringVar Array strings;
Shared NumberVar arrayMax;
Local NumberVar displayNumber := [element in array];
if(arrayMax >= displayNumber) then
strings[displayNumber];
else
u201D;
u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2014u2013
I like this version better because the array is completely dynamic. It can be as big or as small as it needs to be. There is always room for optimizations. But hopefully this will get you started on the right path.
Jenny -
Is there a limitation of characters in a String variable in 8.0.2?
Is there a limitation of characters in a String variable?
I'm trying to create an email within my script and when I paste my text into my parameter, which is a String variable, it will only paste up to 80 characters (including spaces).
In 7.0 I was able to paste my paragraphs and the larges paragraph had 346 characters (including spaces).
Thanks,
DebbieGreg,
Ah.... that's it.
I modified the value of the parameter within the script and added the text that I needed. I added the following:
"To transfer the file over to the router's flash you will need an TFTP or FTP program. Start your application and make sure that the default directory is pointed to the directory where you copied the greeting. Now log into the router that needs to be updated. Once you are in the router issue one of these commands:"
It accepted it and was able to use the entire value. So it must be the web page that will not allow me to enter more then 40 characters.
Do I need to get this submitted as a bug?
Thank you,
Debbie -
Scanning values of characters in a string
Hey guys,
I'm trying to figure out a way I can scan the individual characters of a string to find their value [unicode value, if it's a number, punctuation etc] and I'm a bit stumped. I know I'll have to be using the chatAt method, but I can't think of how I could implement this to continuiously scan through a string.
If someone could help point me in the right direction it would be great.
Thanks in advance.Do you know what a loop is?
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html
Do you know how to find out about methods of the String class such as toCharArray?
http://java.sun.com/javase/6/docs/api/java/lang/String.html -
Specify characters in a string
I'm a beginner at Java.
I need to create a program that will validate only binary characters in a string and count the number of 0s in the string. For some reason I keep getting an error message. For the life of me, I can't figure it out.
char a = '1';
char b = '0';
int hmo;
int totalzeroes=0;
String input = JOptionPane.showInputDialog(null, "Please Enter a Binary Number");
for (hmo=0; hmo<input.charAt(hmo);hmo++)
System.out.println(input.charAt(hmo));
totalzeroes++;
OUTPUT:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range:
at java.lang.String.charAt(Unknown Source)
at cop.main(cop.java:19)...hmo<input.charAt(hmo)...You have to loop up to input.length(), not to the value of the character at certain position.
Mike -
Removing Duplicate Characters in a String without using a Hashset
I was wondering if anyone knew a way of removing duplicate characters in a string without using a Hashset.
I want to go from
helloworld -> helowrdokay, here it is
1) use a loop, look through each character in the original string
2) look at the new string, if this character exist, don't add it, otherwise add it.
check out methods in java.lang.String -
How to count number of repeated characters in a String
I have a String.... 10022002202222.
I need to know how many 2's are there in the string... here the string contains eight 2's..
Thanks in advance..it is workingYes, but... attention to surprises...
SQL> var v1 varchar2(1000);
SQL> exec :v1 := 'How to count the number of occurences of a characters in a string';
PL/SQL procedure successfully completed.
SQL> select length(:v1) - length(replace(:v1,'c')) from dual;
LENGTH(:V1)-LENGTH(REPLACE(:V1,'C'))
6
SQL> exec :v1 := 'cccccc';
PL/SQL procedure successfully completed.
SQL> select length(:v1) - length(replace(:v1,'c')) from dual;
LENGTH(:V1)-LENGTH(REPLACE(:V1,'C'))
SQL> select length(:v1) - nvl(length(replace(:v1,'c')),0) from dual;
LENGTH(:V1)-NVL(LENGTH(REPLACE(:V1,'C')),0)
6
SQL> -
How to search special characters in a string
Hi all,
I want to search special characters in all string columns and all rows in the table.
The table has about 5 string columns and about 5.000.000 rows. For each row and column, I have to search entries, which included special characters like ", !, ? or something else (f.ex. "Mama?Mia" or "!!!Hotel out of order!!!"). The problem, the character could stand at each position in the string.
What's the best and most performance possibility to search such entries?? Is it possibility only by SQL; is there a special function for this?? Or must I use PL/SQL.
Thanks for helping
DanaHTH
Laurent Schneider
OCM DBA
SQL> select * from z;
S
Mama?Mia
a b c
123
SQL> select * from z where translate(s,'~ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz1234567890','~') is not null;
S
Mama?Mia
SQL> select * from z where regexp_like(s, '[^[:alnum:][:space:]]');
S
Mama?Mia -
How do I drop the last 3 characters in a string?
Hello. I'm trying to find a way to drop the last 3 characters in a string. The length varies because it is a last name field. The problem is that when a customer has a suffix on their name, III for example, it is appended to the last name all in the last name field. If I use the ProperCase function, I end up with "Smith Iii". How can I tell it to drop the last 3 characters? I'll then append an UpperCase edition of the last 3 characters to that. Thank you!
Use the instring and right string functions then UpperCase
-
How to find out if there are repeated characters in a string
hey guys
well i kinda have a little problem figuring out how to find out
if there are repeated characters in a string.
if anyone can help, would appreciate it.
thanks
milosTry using the StringTokenizer class. if u already know which character to trace. this could do the job.
eg. String str = "here and there its everywhere";
StringTokenizer st = new StringTokenizer(str, "e");
int rep = st.countTokens(); -
Removing non english characters from my string input source
Guys,
I have problem where I need to remove all non english (Latin) characters from a string, what should be the right API to do this?
One I'm using right now is:
s.replaceAll("[^\\x00-\\x7F]", "");//s is a string having chinese characters.
I'm looking for a standard Solution for such problems, where we deal with multiple lingual characters.
TIA
NitinNitin_tiwari wrote:
I have a string which has Chinese as well as Japanese characters, and I only want to remove only Chinese characters.
What's the best way to go about it?Oh, I see!
Well, the problem here is that Strings don't have any information on the language. What you can get out of a String (provided you have the necessary data from the Unicode standard) is the script that is used.
A script can be used for multiple languages (for example English and German use mostly the same script, even if there are a few characters that are only used in German).
A language can use multiple scripts (for example Japanese uses Kanji, Hiragana and Katakana).
And if I remember correctly, then Japanese and Chinese texts share some characters on the Unicode plane (I might be wrong, 'though, since I speak/write neither of those languages).
These two facts make these kinds of detections hard to do. In some cases they are easy (separating latin-script texts from anything else) in others it may be much tougher or even impossible (Chinese/Japanese). -
Need help in replacing special characters in a string
Hi,
please let me know the best way to replace all the special characters in a string with space.
other than alphabets and numbers
with regards.
sumanth.please let me know the best way to replace all the special characters in a string with space.
other than alphabets and numbers
>
Sumanth Nag Kristam wrote:
> actually i need to replace hexa decimal char 0X1A in a string.... that is 'substitue' as per the chart
> any pointers....
>
> chk the link for the ASCII codes
> http://www.techonthenet.com/ascii/chart.php
But in Hexa decimal value there is no special characters? -
Sample code to identify special characters in a string
Hi,
I need to identify special characters in a string.... could anybody send me some code please.......
Thanks,
Best regards,
Karendata: str(100) type c.
data: str_n type string.
data: str_c type string.
data: len type i.
data: ofst type i.
str = '#ABCD%'.
len = strlen( str ).
do.
if ofst = len.
exit.
endif.
if str+ofst(1) co sy-abcde.
concatenate str_c str+ofst(1) into str_c.
else.
concatenate str_n str+ofst(1) into str_n.
endif.
ofst = ofst + 1.
enddo.
write:/ str.
write:/ str_c.
write:/ 'spacial chracter',20 str_n.
Function module <b>SF_SPECIALCHAR_DELETE</b> <b>DX_SEARCH_STRING</b>
l_address1 = i_adrc-street.
CHECK NOT L_ADDRESS1 IS INITIAL.
len = STRLEN( l_address1 ).
do len times.
if not l_address1+l(1) ca
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '.
if i_adrc-street+l(1) CO sy-abcde.
elseif i_adrc-street+l(1) CO L_NUMCHAR.
exit.
endif.
l = l + 1.
enddo.
data : spchar(40) type c value '~!@#$$%^&()?...'etc.
data :gv_char .
data:inp(20) type c.
take the string length .
len = strlen (i/p).
do len times
MOVE FNAME+T(1) TO GV_CHAR.
IF gv_char CA spchar.
MOVE fnameT(1) TO inp2T(1).
ENDIF.
T = T + 1.
enddo.
REPORT ZEX4 .
PARAMETERS: fname LIKE rlgrap-filename .
DATA: len TYPE i,
T TYPE I VALUE 0,
inp(20) TYPE C,
inp1(20) type c,
inp2(20) type c,
inp3(20) type c.
DATA :gv_char.
data : spchar(20) type c value '#$%^&*()_+`~'.
START-OF-SELECTION.
CONDENSE fname.
len = strlen( fname ).
WRITE:/ len.
DO len TIMES.
MOVE FNAME+T(1) TO GV_CHAR.
IF gv_char ca spchar.
MOVE fnameT(1) TO inpT(1).
ENDIF.
T = T + 1.
ENDDO.
CONDENSE INP.
write:/ 'Special Characters :', inp.
Rewards if useful..........
Minal
Maybe you are looking for
-
Best Practice Regarding Maintaining Business Views/List of Values
Hello all, I'm still in the learning process of using BOXI to run our Crystal Reports. I was never familiar with the BO environment before but I have recently learned that every dynamic parameter we create for a report, the Business View/Data Connec
-
Really hoping someone can help me out with my billing.... I was paying £36 a month for unlimited broadband and weekend and evening calls. In December I was moved onto a deal whereby this would change to £27. Needless to say I have never paid this amo
-
I put money on my itunes account ever since i bought the iphone 5 i havn't been able to purchase any music because they ask for some answers for my sercurtiy questions and i forgot the answers thats why im trying to contact you to see if you can help
-
I want to downgrade IOS 7 to IOS 6.1.3 !!!
I want to downgrade IOS 7 to IOS 6.1.3 !!!
-
?? They expect you to deauthorize from that computer, but if you sell that computer you don't have access to it anymore. HELLO?? Anyone at Adobe try thinking that far ahead or is it done on purpose to screw the customers? I have to active creative cl