Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a string and I want to have that string get split up so that each individual character is in its own string. The string will vary in length as it is user inputted. Thanks in advance
If you actually want an array of strings from a string you can try this
String[] chars = myString.split("");
String str = /*Your String here*/;
char[] charArray = str.toCharArray();
String[] strArray = new String[charArray.length];
String strChars = "";
for (Character c : charArray){
int i=0;
strChars = c.toString();
strArray[i] = strChars;
System.out.println(strChars);
i++;
}
System.out.println(strArray.length);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 days ago.
Improve this question
How can you compare a String coming from a Buffer in Android to a certain string variable? My code is something like this. When I use txtVw.setText(strInput), it shows the string variable strInput, but I cannot compare it to a specific string using an if statement. For example at txtVw the word "CLOSED" is shown but at the if statement it doesn't do anything even if I compared it with a string variable that has a word of "CLOSED".
byte[] buffer = new byte[256];
inputStream.read(buffer);
int i = 0;
for (i = 0; i < buffer.length && buffer[i] != 0; i++) {
}
final String strInput = new String(buffer, 0, i);
txtVw.setText(strInput)
final String str1 = "CLOSED";
if (strInput.equals(str1)){
// *do something*
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a String with value as:
String lstr = [12.88, 77.56],[12.81, 77.7156]
....so on
I need to parse and iterate it and somehow substitute the values as :
final ArrayList<Coordinate> points = new ArrayList<Coordinate>();
points.add(new Coordinate(12.88, 77.56));
points.add(new Coordinate(12.81, 77.7156));
i tried converting string to List and then iterating it using for loop, but it is not working, either it goes out of bound or extra square bracket throws an exception.
What is the best way to parse, format and iterate a string like this?
You can do something like in the example below. Note you can improve your regex to check for spaces etc.:
String lstr = "[12.88, 77.56],[12.81, 77.7156]";
List<Coordinate> cors = new ArrayList<Coordinate>();
String []coordinates = lstr.split("\\],\\[");
for(String cordinate:coordinates)
{
String []xy = cordinate.split(",");
cors.add(new Coordinate(xy[0],xy[1]));
}
System.out.println(cors);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
"Sales Docket successfully saved and sent for approval. Please note your document number. JBHL/39/16-17"
i want only the number 39 in the string should be increased by +1 when we run the method
Use regular expression to find the number, then build new string:
private static String increment(String input) {
Matcher m = Pattern.compile("/(\\d+)/").matcher(input);
if (! m.find())
throw new IllegalArgumentException("Invalid document number: " + input);
int newNumber = Integer.parseInt(m.group(1)) + 1;
return input.substring(0, m.start(1)) + newNumber + input.substring(m.end(1));
}
Test
System.out.println(increment("JBHL/39/16-17"));
Output
JBHL/40/16-17
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My input String is as below,
{"string name"=hi;"id"=44401234;"string name"=hey;"id"=87695432.....}
I want only the ids as output like, {44401234 87695432}
for(int i=0;i<input.length();i++)
{
int index=input.indexOf("id");
hh=input.substring(index+4,index+12);
System.out.println(hh);
}
The below code uses Regex to get the value associated with id
try
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String input = "{\"string name\"=hi;\"id\"=44401234;\"string name\"=hey;\"id\"=87695432.....}";
Pattern pattern = Pattern.compile("\"id\"=[0-9]{8}");
Matcher m = pattern.matcher(input);
while (m.find()) {
String str = m.group();
str = str.replace("\"id\"=", "");
System.out.println(str);
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm getting email address like user.q#stackoverflow.com. I want to add string before '#' symbol of email address in java.
After added string in the email address : user.qzzz#stackoverflow.com. How to insert 'zzz' string before # symbol in java ?
String input = "zzz";
String email = "user.q#stackoverflow.com";
int at = email.indexOf('#');
String newEmail = email.substring(0, at) + input + email.substring(at);
This will find the index of the at-sign:
int index = emailString.indexOf("#");
This will give you the first part of the string, before the at-sign:
String firstPart = emailString.substring(0, index);
This will give you the end part, starting with the at-sign
String lastPart = emailString.substring(index);
Put the bits together with your new stuff to make the result.
This should work, though not super efficient:
String s = email.substring(0, email.indexOf("#")-1)+"ZZZ"+email.substring(email.indexOf(#));
Another solution is using the split method of strings to get both sides of the string.
String email = "user.q#stackoverflow.com";
String[] strList = email.split("#");
strList[0] += "zzz";
email = strList[0] + "#" + strList[1];
System.out.println(email);