How to perform string manipulation using replace, substring, split, etc - java

I have a string data like this
string1 = ["car","house","boat"]["one","two","three","four"]["tiger","cat"]
I want the output to be like this :
first : car,house,boat
second : one,two,three,four
third : tiger,cat
How should I perform manipulation on that string?
This is my current attempt:
result6 = string1.substring(1);
String[] parts = result6.split("\\[");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
result3 = part1.replaceAll("[^a-zA-Z0-9,]", "");
result4 = part2.replaceAll("[^a-zA-Z0-9,]", "");
result5 = part3.replaceAll("[^a-zA-Z0-9,]", "");
result1 = "first : " + part1 + "\n" + "second : " + part2 + "\n" + "third : \n" + part3;
But that gets me erroneous output.

You have added part1, part2, part3 instead of result3, 4, 5 in the result1 assignment part. And also i suggest you to split according to ][, i you do the split according to [ only, you would get an empty string in parts[0].
String string1 = "[\"car\",\"house\",\"boat\"][\"one\",\"two\",\"three\",\"four\"][\"tiger\",\"cat\"]";
String parts[] = string1.split("\\]\\[");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
String result3 = part1.replaceAll("[^a-zA-Z0-9,]", "");
String result4 = part2.replaceAll("[^a-zA-Z0-9,]", "");
String result5 = part3.replaceAll("[^a-zA-Z0-9,]", "");
String result1 = "first : " + result3 + "\n" + "second : " + result4 + "\n" + "third : " + result5;
System.out.println(result1);
Output:
first : car,house,boat
second : one,two,three,four
third : tiger,cat

Related

Stop words not being correctly removed from string

I have a function which reads stop words from a file and saves it in a HashSet.
HashSet<String> hset = readFile();
This is my string
String words = "the plan crash is invisible";
I am trying to remove all the stop words from the string but it is not working correctly
The output i am getting: plan crash invible
Output i want => plan crash invisible
Code:
HashSet<String> hset = readFile();
String words = "the plan crash is invisible";
String s = words.toLowerCase();
String[] split = s.split(" ");
for(String str: split){
if (hset.contains(str)) {
s = s.replace(str, "");
} else {
}
}
System.out.println("\n" + "\n" + s);
While hset.contains(str) matches full words, s.replace(str, ""); can replace occurrences of the "stop" words which are part of words of the input String. Hence "invisible" becomes "invible".
Since you are iterating over all the words of s anyway, you can construct a String that contains all the words not contained in the Set:
StringBuilder sb = new StringBuilder();
for(String str: split){
if (!hset.contains(str)) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(str);
}
}
System.out.println("\n" + "\n" + sb.toString());
No need so check if your string contain the stop word or split your string, you can use replaceAll which use regex, like this :
for (String str : hset) {
s = s.replaceAll("\\s" + str + "|" + str + "\\s", " ");
}
Excample :
HashSet<String> hset = new HashSet<>();
hset.add("is");
hset.add("the");
String words = "the plan crash is invisible";
String s = words.toLowerCase();
for (String str : hset) {
s = s.replaceAll("\\s" + str + "|" + str + "\\s", " ");
}
s = s.replaceAll("\\s+", " ").trim();//comment and idea of #davidxxx
System.out.println(s);
This can gives you :
plan crash invisible

Break up the text on the page

I have a string
String str = "line1"+"\n" +
"line2"+"\n" +
"line3"+"\n" +
"line4"+"\n" +
"line5"+"\n" +
"line6"+"\n" +
"line7"+"\n" +
"line8"+"\n" +
"line9"+"\n" +
"line10"+"\n" +
"line11"+"\n" +
"line12"+"\n" +
"line13"+"\n" +
"line14"+"\n" +
"line15"+"\n" +
"line16"+"\n" +
"line17"+"\n";
I want to get out of it an array of strings
String str1 = "line1"+"\n" +
"line2"+"\n" +
"line3"+"\n" +
"line4"+"\n";
String str2 = "line5"+"\n" +
"line6"+"\n" +
"line7"+"\n" +
"line8"+"\n";
String str3 = "line9"+"\n" +
"line10"+"\n" +
"line11"+"\n" +
"line12"+"\n";
String str4 = "line13"+"\n" +
"line14"+"\n" +
"line15"+"\n" +
"line16"+"\n";
String str5 = "line17"+"\n";
if I do so
String[] str1 = str.split("\n");
I get an array of strings, in which only one line, and I need it for a few
instead of the string I will have the file from which I plan to read the text in a row
for splitting string with particular format you need to specify regular expression
so in your case regular expression will be ("\r\n")
Look Here

java regex to match variable

I have the name of a java variable in a string. I want to replace it with the letter x. How can I do this java, and make sure that other words in the string are not replaced ?
For example, say my variable is res, and my string is "res = res + pres + resd + _res. I want the string to become x = x + pres + resd + _res.
You can use a word boundary to only capture whole words:
String s = "res = res + pres + resd + _res";
String var = "res";
System.out.println(s.replaceAll("\\b" + var + "\\b", "x"));
outputs x = x + pres + resd + _res
You can use the \b metacharacter to match a word boundary. (Bear in mind you'll need to use doule backslashes to escape this in Java.)
So you can do something like the following:
final String searchVar = "res";
final String replacement = "x";
final String in = "res = res + pres + resd + _res";
final String result = in.replaceAll("\\b" + searchVar + "\\b", replacement);
System.out.println(result);
// prints "x = x + pres + resd + _res"

How to add # symbol to string in android?

I am working on android. In my project I am adding '|' symbol and '#' symbol to string but it is not adding # symbol. I am not
getting where I went wrong. Please help me with this.
String str="";
str = str + "|" + id + "#" + id2 + "#" + id3;
When I print the string "str" it is displaying only id value but it is not printing id1 and id2.
Output:
|13
Use this:
<string name="strings">\#Strings</string>
i have check it like
String id = "A";
String id2 = "B";
String id3 = "C";
String str="";
str = str + "|" + id + "#" + id2 + "#" + id3;
and Output Like
|A#B#C
I tried this,
TextView tvAt = (TextView)findViewById(R.id.tvAtExample);
String id1 = "A";
String id2 = "B";
String id3 = "C";
tvAt.setText(id1 + "|"+ id2 + "#" + id3);
Its working, It produces the output
A|B#C

Split string on spaces and dash but not on the value of the string

I am setting a date_string like this:
gridcell.setTag(theday + "-" + themonth + "-" + theyear + "|" + hijri_day + "-" + hijri_month + " ("+ hijri_monthno +") " + hijri_year);
And I am splitting it like this:
String date_month_year = (String) view.getTag();
String[] dateAr = date_month_year.split("-|\\||\\(|\\)|\\s+");
This is also splitting the spaces and dash in the hijri month names i.e. Rabi al-Thani or Dhul Hijjah:
private String months[] = {"Muharram","Safar","Rabi al-Awwal","Rabi al-Thani","Jumada al-Awwal","Jumada al-Thani","Rajab","Sha\'ban","Ramadhan","Shawwal","Dhul Qa\'dah","Dhul Hijjah"};
How do I split on the date_string only and not the value of the strings in the date_string?
best way is changing the date separator - to / (slash) or .(dot) If you really wanna keep like this, than after split you can check last character on string array if it is a letter join that two string into one back..
gridcell.setTag(theday + "." + themonth + "." + theyear + "|" + hijri_day + " " + hijri_month + " ("+ hijri_monthno +") " + hijri_year);
make it like this easiest way..
I tried to split your date step by step so check if this works for you
List<String> tokens=new ArrayList<String>();
String data="theday-themonth-theyear|hijri_day-Dhul Qa\'dah (hijri_monthno) hijri_year";
String[] tmp = data.split("\\|");
//System.out.println(Arrays.toString(tmp));
for (String s:tmp[0].split("-"))
tokens.add(s);
System.out.println(tokens);// -> [theday, themonth, theyear]
String[] tmp2=tmp[1].split("\\s*\\(|\\)\\s*");
//System.out.println(Arrays.toString(tmp2));
for (String s:tmp2[0].split("-",2))
tokens.add(s);
System.out.println(tokens);// -> [theday, themonth, theyear, hijri_day, Dhul Qa'dah]
tokens.add(tmp2[1]);
tokens.add(tmp2[2]);
System.out.println(tokens);// -> [theday, themonth, theyear, hijri_day, Dhul Qa'dah, hijri_monthno, hijri_year]

Categories

Resources