This question already has answers here:
how to get data between quotes in java?
(6 answers)
Closed 6 years ago.
I have a string of names. I am looking to split it based on names between double quotes. I used the following code to split the names.
String []splitterString=str.split("\"");
for (String s : splitterString) {
System.out.println(s);
}
I get the output as:
[
Hossain, Ziaul
,
Sathiaseelan, Arjuna
,
Secchi, Raffaello
,
Fairhurst, Gorry
]
I need to store just the names from these. I am not sure how to do that.
This is the string:
["Hossain, Ziaul","Sathiaseelan, Arjuna","Secchi, Raffaello","Fairhurst, Gorry"]
Thanks for the help!!!
I think following solution may help you out :-
String str = "[\"Hossain, Ziaul\",\"Sathiaseelan, Arjuna\",\"Secchi, Raffaello\",\"Fairhurst, Gorry\"]";
String [] str1 = str.split("\\[\"|\",\"|\"\\]");
for (int iCount = 0; iCount < str1.length; iCount++)
{
System.out.println(str1[iCount]);
}
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I have trouble with my code. I need to replace element in Array if condition is true.
Inputs are:
dashedCapital = "------";
input = "a";
capital = "Warsaw";
Code should check if capital contains input and if yes replace "-" in dashedCapital to character from input at specified position:
public static String changeDashedCapital(String dashedCapital, String input, String capital){
String[] capitalArray = capital.split("");
String[] dashedCapitalArray = dashedCapital.split("");
String[] character = input.split("");
for(int i = 0; i < capitalArray.length; i++){
//System.out.println(i);
//System.out.println(capitalArray[i] + character[0] + dashedCapitalArray[i]);
if(capitalArray[i] == character[0]){
dashedCapitalArray[i] = character[0];
}
}
String result = Arrays.toString(dashedCapitalArray);
System.out.println(result);
return result;
}
Result is "------" but should be "-a--a-". What's going wrong?
John, thanks for your reply, it was helpful.
I edited my method so it's look like this now:
public static String changeDashedCapital(String dashedCapital, String input, String capital){
for(int i = 0; i < capital.length(); i++){
if(capital.charAt(i).equals(input.charAt(0))) {
String new_dashed = dashedCapital.substring(0,i)+input.charAt(0)+dashedCapital.substring(i);
System.out.println(new_dashed);
}
}
return "OK:";
Now i get this error:
GetWord.java:69: error: char cannot be dereferenced
if(capital.charAt(i).equals(input.charAt(0))) {
^
1 error
I don't understand why it's wrong. I using a equals() function. I also tried "==" operator but then nothing happens. What does it mean "char cannot be dereferenced"? How I could compare single chars from string with another chars from another string?
The reason it is not working is because your if for character equality is never true. You’re comparing strings of length 1 and not characters. You can quickly fix by changing if be using the string comparing function .equals()
if(capitalArray[i].equals(character[0])){
...
}
However, you should change your code and not just use this fix. Don’t split your stings into arrays, just use the .charAt() method to get a character at a particular index.
This question already has answers here:
How do I count the number of occurrences of a char in a String?
(48 answers)
Closed 6 years ago.
I have below mentioned string
String str = "\nArticle\n\nArticle\nArticle";
I want total number of count. How can i get this?
As the string contain \n so always it gives 1 instead of 3
To get you started, I will show you a simple example:
String str = "\nArticle\n\nArticle\nArticle";
// Split the String by \n
String[] words = str.split("\n");
// Keep the count of words
int wordCount = 0;
for(String word : words){
// Only count non-empty Strings
if(!word.isEmpty()) {
wordCount++;
}
}
// Check, answer is 3
System.out.println(wordCount);
This question already has answers here:
What Does The Colon Mean In Java?
(5 answers)
Closed 7 years ago.
I have a string handling related question
split() - The return type is String[]
In this for loop we are storing the split value in a String literal
for (String retval: Str.split("-"))
Why doesn't it give a type mismatch error like in the code below?
String Str1 = "abfg-hjddh-jdj";
String Str = Str1.split("-");
String Str = Str1.split("-");
gives error because split returns an array, so the correct syntax is:
String[] Str = Str1.split("-");
In a for-each loop
for (String retval : Str.split("-"))
For each loop : indicates you will be iterating an array, collection or list of Strings, so no error is trhown
Examples:
for (int retval : string.split("-")) // error,
ArrayList<Books> books;
for (Book book : books) // correct
Set<Integer> integers;
for (Integer mInt : integers) // correct
for (String mInt : integers) // incorrect!!!
LAST BUT NOT LEAST: variables in Java should start with LOWERCASE, please check Code Conventions.
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
I have a delimited string and The delimiter is "$%". The fields are in an specific order. For example: John$%Smith$%30$%Los Angeles
I need to get the values of this string and store them in the respective property
Customer.firstName(_)
Customer.lastName(_)
Customer.age(_)
Customer.city(_)
So far I have tried this but I know I am doing it incorrectly:
if(thisString != null){
if(thisString.endsWith("$%")){
Customer.firstName(thisString.substring(0,indexOf("$%");
}
}
Trying using Java's String.split
For example:
//Split string based on "$%"
String[] values = thisString.split(Pattern.quote("$%"));
Customer.firstName() = values[0]; //Set first name equal to first string from the split
//etc
This question already has answers here:
Get string character by index
(13 answers)
Closed 8 years ago.
i want to print first character from multiple word , this word coming from api , like
DisplayName: arwa othman .
i want to print the letter (a) and (o).
can anyone to help me please ??
Try this
public String getFirstWords(String original){
String firstWord= "";
String[] split = original.split(" ");
for(String value : split){
firstWord+= value.substring(0,1);
}
return firstWord;
}
And use this as
String Result = getFirstWords("arwa othman");
Edit
Using Regex
String name = "arwa othman";
String firstWord= "";
for(String s : name.split("\\s+")){
firstWord += s.charAt(0);
}
String Result = firstWord;
You can use the the Apache Commons Langs library and use the initials() method , you can get more information from here http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html#initials(java.lang.String)
I am quoting a sample code snippet that might be useful :
WordUtils.initials(null) = null
WordUtils.initials("") = ""
WordUtils.initials("Ben John Lee") = "BJL"
WordUtils.initials("Ben J.Lee") = "BJ"
This may work for you:
String[] splitArray = displayName.split("\\s+");
char[] initials = new char[splitArray.length];
for (int i = 0; i < splitArray.length; i++) {
initials[i] = splitArray[i].charAt(0);
}
This will give you a char array. If you want a String array use String.valueOf(char)