0
votes
4answers
42 views

Best way to get new string from existing string

I need to re arrange the String and get a new String. Below are the details Current String = 2014-10-15 New String = 20141015 I found that we can use split or sub string for this. What is the best ...
4
votes
4answers
57 views

Showing email address as hint

I have seen Mail services displays the email id's as e*****[email protected] , mostly in their recovery page. So i am trying to replace the [email protected] as e*****[email protected] . Is it possible to ...
0
votes
3answers
35 views

Reducing the number of comparisons performed when matching between two String arrays

I am comparing three arrays of Strings using the two classes below. Without using any hash maps or changing the structure of my code too much (I can't change the signature of findMatchingElements()), ...
0
votes
2answers
30 views

Check to see if future objects are equal

So my question is this: Can I equate any two objects even if one of those is an object that may be constructed later? Eg: In the code below I have a movie creation class and that's all well and ...
2
votes
1answer
31 views

StringIndexOutOfBoundsException on very large string

I am trying to parse a String that is the HTML from a page from my web browser application, for the HTML that is the data that I am trying to retrieve. (In short, I am doing some web-scraping of ...
2
votes
2answers
42 views

Formatting String Array efficiently in Java

I was working on some string formatting, and I was curious if I was doing it the most efficient way. Assume I have a String Array: String ArrayOne[] = {"/test/" , "/this/is/test" , "/that/is/" ...
0
votes
1answer
29 views

Using recursion to parse a binary number as string into a decimal

So the goal is to get a String that is input. You then use bin2Dec method to parse that string (which is binary) into a decimal number. Here is what I have so far. I do not know how to take the ...
0
votes
3answers
34 views

How would I extract integers from a string and add them together in Java without the split method/arrays?

Say I am given a string like (1 5 23 100 90). How would I extract them, convert them to integers, and then add them together? I believe Integer.parseInt(String) will convert them into integers, but ...
-3
votes
2answers
66 views

How do I declare two or more data types on the same line??(New to programming)

I am having difficulty combining two data types on the same line for simplicity purposes... Here is my current code: public class countryPopulations{ static String zero = "Canada "; static ...
-5
votes
2answers
66 views

Get String from another class

I am trying to get the value of a String from another class and add it to a String in that class. I have tried getting it by methods and returns and other ways but it is not working! When the user ...
0
votes
0answers
15 views

Getting Field Instance in String Message similar to TibrvMsg

I am moving my implementation from TibrvMSg to a String msg. My message contains few fields which have the same name. The logic of parsing them depends on the fieldinstance. for ex: ...
-1
votes
2answers
33 views

Read text from a string & edit it

Maybe there is already a similar question, but I didn't find it. So, how can I "read" a string and edit it? For example: String exampleString = "Monday: Do stuff & things!"; Now, I want to ...
-1
votes
5answers
45 views

How do I use the string value from one class in another class?

I'm very new to Java and some help would be appreciated! When I compile/run "Driver.java", it will ask the user to type in their name. And what the program's supposed to do is take that name and pass ...
2
votes
1answer
81 views

Split a String on an Integer followed by a space

I have a rather large String that i need to split so I can put it into an array. As it is, there will be a semicolon followed by an Integer, followed by a space and this is where I need to split it. ...
2
votes
3answers
3k views

Android Preferences error, “String cannot be cast to int”

I'm trying to setup a preferences activity but my app keeps crashing and I get the following logcat: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ...
-1
votes
1answer
87 views

Create String binary terminator

I need to place terminator to my string. Terminator might be byte 11 (or 0x0B) or possible any other in future. What is the best way to add byte to my string? I would like to do something like: ...
33
votes
3answers
14k views

Java String split removed empty values

I am trying to split the Value using a separator. But I am finding the surprising results String data = "5|6|7||8|9||"; String[] split = data.split("\\|"); System.out.println(split.length); I am ...
4
votes
4answers
3k views

How to get only part of URL from HttpServletRequest?

From the following URL I need to get (http://localhost:9090/dts) alone. That is I need to remove (documents/savedoc) (OR) need to get only - (http://localhost:9090/dts) ...
20
votes
5answers
17k views

Best practices/performance: mixing StringBuilder.append with String.concat

I'm trying to understand what the best practice is and why for concatenating string literals and variables for different cases. For instance, if I have code like this StringBuilder sb = new ...
11
votes
6answers
34k views

Defining constant string in Java?

I have a list of constant strings that I need to display at different times during my Java program. In C I could define the strings like this at the top of my code #define WELCOME_MESSAGE "Hello, ...
11
votes
2answers
16k views

Get the index of a pattern in a string using regex

I want to search a string for a specific pattern. Do the regular expression classes provide the positions (indexes within the string) of the pattern within the string? There can be more that 1 ...
46
votes
18answers
44k views

String is immutable. What exactly is the meaning?

I wrote the following code on Immutable Strings. public class ImmutableStrings { public static void main(String[] args) { testmethod(); } private static void testmethod() { ...
11
votes
5answers
45k views

How do I use StringUtils in Java?

I'm a beginner in Java. I want to use StringUtils.replace but Eclipse outputs "StringUtils cannot be resolved". I tried import java.lang.*;, but it doesn't work.
45
votes
5answers
21k views

Converting Integer to String with comma for thousands

I want to convert an Integer 35634646 to have the thousand "," so it should be 35,634,646. What would be the quickest way to doing that?
55
votes
4answers
87k views

Strip Leading and Trailing Spaces From Java String [duplicate]

Possible Duplicate: trim whitespace from a string? Is there a convenience method to strip any leading or trailing spaces from a Java String? Something like: String myString = " keep this ...
664
votes
17answers
1.5m views

How to convert string to int in Java?

How does one convert a String to an int in Java? I have a string which contains only numbers (the numbers 1-4 to be specific), and I want to return the number which it represents (actually I have a ...
4
votes
3answers
28k views

Check if string contains \n Java

How do I check if string contains \n or new line character ? word.contains("\\n") word.contains("\n")
13
votes
8answers
9k views

What is an efficient way to replace many characters in a string?

String handling in Java is something I'm trying to learn to do well. Currently I want to take in a string and replace any characters I find. Here is my current inefficient (and kinda silly IMO) ...
1
vote
6answers
6k views

How to know if a given string is substring from another string in Java

Hi I have to compute if a given string is substring of a bigger string. For example String str = "Hallo my world"; String substr = "my" The method "contains" should return true because str ...
20
votes
6answers
16k views

Most efficient way to make the first character of a String lower case?

Given an input String, what is the most efficient way to make just the first character lower case? I can think of a number of ways to do this. For example, using charAt and subString: String string= ...
140
votes
6answers
389k views

Java int to String - Integer.toString(i) vs new Integer(i).toString()

Sometimes java puzzles me. I have a huge amount of int initializations to make. What's the real difference? Integer.toString(i) new Integer(i).toString()
62
votes
5answers
102k views

Android Split string

I have a string called CurrentString and is in the form of something like this "Fruit: they taste good". I would like to split up the CurrentString using the : as the delimiter.So that way the word ...
255
votes
8answers
672k views

How to use java.String.format in Scala?

I am trying to use a .format method of a string. But if I place %1, %2, etc. in the string, java.util.UnknownFormatConversionException is thrown pointing to a confusing Java source code piece: ...
32
votes
8answers
27k views

Fastest way to check if a List<String> contains a unique String

Basically I have about 1,000,000 strings, for each request I have to check if a String belongs to the list or not. I'm worried about the performance, so what's the best method? ArrayList? Hash?
58
votes
7answers
65k views

Java how to replace 2 or more spaces with single space in string and delete leading spaces only

Looking for quick, simple way in Java to change this string " hello there " to something that looks like this "hello there" where I replace all those multiple spaces with a single space, ...
27
votes
11answers
83k views

What is the difference between String and StringBuffer in Java?

What is the difference between String and StringBuffer in Java? Is there a maximum size for String?
124
votes
22answers
111k views

Capitalize First Char of Each Word in a String Java

Is there a function built into Java that capitalizes the first character of each word in a String, and does not affect the others? Examples: jon skeet -> Jon Skeet miles o'Brien -> Miles O'Brien (B ...
18
votes
9answers
15k views

Fast algorithm for searching for substrings in a string

I'd like an efficient algorithm (or library) that I can use in Java to search for substrings in a string. What I would like to do is: Given an input string - INSTR: "BCDEFGH" And a set of ...
79
votes
9answers
59k views

Splitting a comma-separated string but ignoring commas in quotes

I have a string vaguely like this: foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy" that I want to split by commas -- but I need to ignore commas in quotes. How can I do this? Seems like a regexp ...
65
votes
7answers
114k views

How to convert String object to Boolean Object?

How to convert String object to Boolean object?
38
votes
2answers
90k views

What are all the escape characters in Java?

I know some of the escape characters in Java, e.g. \n : Newline \r : Carriage return \t : Tab \\ : Backslash ... Is there a complete list somewhere?
38
votes
3answers
114k views

How do I find out if first character of a string is a number?

In Java is there a way to find out if first character of a string is a number? One way is string.startsWith("1") and do the above all the way till 9, but that seems very inefficient. Edit: ...
193
votes
33answers
160k views

Java multiline string

Coming from Perl, I sure am missing the "here-document" means of creating a multi-line string in source code: $string = <<"EOF" # create a three line string text text text EOF In Java I have ...
31
votes
14answers
9k views

Best Language for String Manipulation? [closed]

I am about to begin writing an app that handles adding new users/repostories to my subversion server, so that I don't have to repeatedly open vi and edit conf files and execute shell commands. Most ...
730
votes
23answers
788k views

How do I compare strings in Java?

I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug. Is == bad? When should it ...
525
votes
8answers
451k views

Why can't I switch on a String?

Why can't I switch on a String? Is this functionality going to be put into a later Java version? Can someone point me to an article, or themselves explain why I can't do this, as in, the technical ...
1256
votes
34answers
686k views

Read/convert an InputStream to a String

If you have java.io.InputStream object, how should you process that object and produce a String? Suppose I have an InputStream that contains text data, and I want to convert this to a String (for ...
236
votes
9answers
249k views

How do I split a string with any whitespace chars as delimiters?

What regex pattern would need to pass to the java.lang.String.split() method to split a string with all whitespace characters (' ', '\t', '\n', etc.) as delimiters?
76
votes
9answers
209k views

How to convert Strings to and from UTF8 byte arrays in Java

In Java, I have a String and I want to encode it as a byte array (in UTF8, or some other encoding). Alternately, I have a byte array (in some known encoding) and I want to convert it into a Java ...
119
votes
13answers
115k views

Is the Contains Method in java.lang.String Case-sensitive?

Say I have 2 strings, String s1 = "AbBaCca"; String s2 = "bac"; I want to preform a check returning that s2 is contained within s1. I can do this with: return s1.contains(s2); I am pretty sure ...