Printing only string - java

Dear All,
I have 2 input string 1)stack,over,flow 2)stack/over,flow,com
I would like to print only strings(without special char)from the above 2 input
for 1st input i used below function but for 2nd input i dont know how to process it.Pls give me solution.
st = new StringTokenizer("stack,over,flow", ",");
while (st.hasMoreTokens())
{
String token = st.nextToken();
System.out.println("token = " + token);
}
output:
stack
over
flow

Can you define what you mean by a special char? Do you mean "only alphanumeric characters"?
If you want to remove all possible special characters and leave only alphanumeric characters, then you can use the following:
s = s.replaceAll("[^a-zA-Z0-9]", "");
If you want to print them one by one:
s = s.replaceAll("[^a-zA-Z0-9]", "/");
String[] splitString = s.split("/");
for(String str:splitString)
{
System.out.println(str);
}
EIDT:
Since you asked: the above code uses the enhanced for loop (also called the for-each loop) introduced by java 5.
Using the 'normal' for loop, this is:
s = s.replaceAll("[^a-zA-Z0-9]", "/");
String[] splitString = s.split("/");
for(int i=0; i<splitString.length(); i++)
{
String str = splitString[i];
System.out.println(str);
}
Also:
StringTokenizer is a legacy class
that is retained for compatibility
reasons although its use is
discouraged in new code. It is
recommended that anyone seeking this
functionality use the split method
of String or the java.util.regex
package instead.
(from the javadoc)

You need to format your question better, it's hard to follow. But if I read it correctly,
st = new StringTokenizer("stack,over,flow", ",/");
Should work just fine.

This should work for you in both the cases:
String[] strArr=yourString.split("[/,]");
for(String str:strArr)
{
System.out.println(str);
}

Related

Split a string of multiple sentences into single sentences and surround them with html tags

I am a Java beginner and currently looking for a method to Split a String message into substrings, based on delimiter ( . ). Ideally I have single sentences then and I want to wrap each sentence in HTML tags, i. e. <p></p>.
I tried the following with BreakIterator class:
BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.ENGLISH);
List<String> sentences = new ArrayList<String>();
iterator.setText(message);
int start = iterator.first();
String newMessage= "";
for (int end = iterator.next();
end != BreakIterator.DONE;
start = end, end = iterator.next()) {
newMessage= "<p>"+ message.substring(start,end) + "</p>";
sentences.add(newMessage);
}
This gives back one sentence. I am stuck here, I also want to wrap each number in a each sentence.
The String I have contains something like:
String message = "Hello, John. My phone number is: 02365897458.
Please call me tomorrow morning, at 8 am."
The output should be:
String newMessage = "<p>Hello, John.</p><p>My phone number is:
<number>02365897458</number>.
</p><p>Please call me tomorrow morning, at 8 am.</p>"
Is there a possibility to achieve this?
Try the split method on Java String. You can split on . and it will return an array of Strings.
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-
This can easily be done using the StringTokenizer class, along with the StringBuilder class:
String message = SOME_STRING;
StringBuilder builder = new StringBuilder();
StringTokenizer tokenizer = new StringTokenizer(message, ".");
while(tokenizer.hasMoreTokens()) {
builder.append("<p>");
builder.append(tokenizer.nextToken());
builder.append("</p>");
}
return builder.toString();
You can add more delimiters as required for various tags.
Surrounding sentences could be archived by adding a <p> at the start, a </p> at the end and replacing each full-stop with .</p><p>. Take a look at the replace method for strings.
And to add the number tag, you could use a regex replace. The replaceAll method and a regex like [0-9]+, depending on what your numbers look like, can do that.
Something similar to this should work (untested):
newMessage = "<p>" + message.replace(".", ".</p><p>")
.replaceAll("([0-9]+)", "<number>$1</number>") +
"</p>"
As said above, you can use the split method. Because you're splitting on dots be sure to escape this in your regex. A simple example (there are other ways to keep the delimiter but I've done it like this for simplicity as you're beginning);
String toSplit = "Hello, John. My phone number is: 02365897458. Please call me tomorrow morning, at 8 am.";
String[] tokens = toSplit.split("\\.");
for(String token : tokens) {
token = "<p>" + token + ".</p>";
}

Tokenizer java (string)

For example, Ive got Some stringa.b.c.d`, I have separated it by tokens. but now I want to do a specific thing with a, then something else with b, and something else with c. How can I do that?
String value="a.b.c.d";
StringTokenizer tokenize = new StringTokenizer(value, ".");
while(tokenize.hasMoreTokens()){
String separated1= tokenize.?????;
String separated2= tokenize.?????;
String Val1 = someMethod1(separated1);
String Val2 = someMethod2(separated2);
}
//tokenize next line is not solution
Just to spell out what #RealSkeptic already said, avoid the loop:
String value = "a.b.c.d";
String[] separated = value.split("\\.");
if (separated.length >= 2) {
String val1 = someMethod1(separated[0]);
String val2 = someMethod2(separated[1]);
} else {
// other processing here
}
You could do something similar with a StringTokenizer if you liked, only you would need at least two if statements to check whether there were enough tokens. But as #user3437460 said, StringTokenizer is now legacy; String.split() is recommended in new code.
Using String.split you can split about the . into an array.
How about something like this which will print each token:
String value = "a.b.c.d";
String[] tokens = value.split("\\.");
for (String token : tokens) {
System.out.println(token);
}
The . needs to be escaped as the split function takes a regexp and . is a wildcard
Why don't use split ? Easier to use and makes more sense here

How to split a string by comma followed by a colon in java?

I'm a java newbie and I'm curious to know how to split a string that starts with a comma and gets followed by a colon towards the end.
An example of such string would be?
-10,3,15,4:38
5,15,8,2:8
Could it be like this?
sections = line.split(",");
tokens = sections[3].split(":");
or is it even possible to split line which the file is read into twice?
tokens = line.split(",");
tokens = line.split(":");
I also tried this but it gave me an ArrayOutOfBound error
tokens = line.split("[,:]");
Any contribution would be appreciated.
use a regular expression in the split section such as
line.split(",|;");
Haven't tested it but I think you get the idea.
You can also do it this way, if you want it for a general case, the method basically takes in the string array, splits each string at each index in the array and adds them to an ArrayList. You can try it, it works.
public static void splitStrings(String[] str){
String[] temp1 =null;//initialize temp array
List<String> itemList = new ArrayList<String>();
for(int i=0;i<str.length;i++){
temp1=str[i].split(",|:");
for (String item : temp1) {
itemList.add(item);
}
//Only print the final result of collection once iteration has ended
if(i==str.length-1){
System.out.println(itemList);
}
}
I am not sure if I totally understand your question correctly. But if you first want to split by , and then by :, you can call split() function twice
String[] str = {"-10,3,15,4:38", "5,15,8,2:8"};
for (String s: str) {
String[] temp = s.split(",")[3].split(":");
System.out.println(temp[0] + " " + temp[1]);
}
Output:
4 38
2 8

Java Splitting A Sentence

I am writing a program for Twitter. It will read a tweet and get the hashtags in it.
The problem is, I couldn't split it. For example, "I love #computers so much." in this one, I need to obtain only the "computers" part.
I thought about using split function by using # but it will split the sentence in a half so still, it won't be a solution. Any ideas?
You want to split on the # indeed. After that you want to have the word. So split on the " " space :).
string="I love #computers so much.";
String[] parts = string.split("#");
String part1 = parts[0]; // I love
String part2 = parts[1]; // computers so much.
String[] parts2 = part2.split(" ");
String output = parts2[0];
The above should work, haven't tested it though.
If there are multiple hashtages the above won't work, try the below one:
String string="I love #computers so #much omg #lol .";
String[] stringParts = string.split("#");
//'delete' first element.
String[] parts = Arrays.copyOfRange(stringParts, 1, stringParts.length);
int i = 0;
String[] output = new String[10];
for(String part : parts)
{
if(part.contains(" "))
{
String[] parts2 = part.split(" ");
output[i] = parts2[0];
i++;
}
}
The only problem is with this code, that you need a space otherwise you will have different characters in your word.
You would do well to take a look at solving the problem using regular expressions.... try something like (?<=#)\w+ -- it will return all alpha numerics after the #, while not capturing the #. You may want to change the \w to include additional characters as required. Hope this helps.
You can use regular expressions to obtain the hash tags from the tweet. Something like:
String sentence = "I love #computers and #something_Else so much";
Pattern p = Pattern.compile("#\\S+");
List<String> hashTags = new ArrayList<>();
Matcher matcher = p.matcher(sentence);
while (matcher.find()) {
hashTags.add(matcher.group(0));
}
System.out.println(hashTags);

how to check if string contains '+' character [duplicate]

This question already has answers here:
How can I check if a single character appears in a string?
(16 answers)
Closed 6 years ago.
I want to check if my string contains a + character.I tried following code
s= "ddjdjdj+kfkfkf";
if(s.contains ("\\+"){
String parts[] = s.split("\\+);
s= parts[0]; // i want to strip part after +
}
but it doesnot give expected result.Any idea?
You need this instead:
if(s.contains("+"))
contains() method of String class does not take regular expression as a parameter, it takes normal text.
EDIT:
String s = "ddjdjdj+kfkfkf";
if(s.contains("+"))
{
String parts[] = s.split("\\+");
System.out.print(parts[0]);
}
OUTPUT:
ddjdjdj
Why not just:
int plusIndex = s.indexOf("+");
if (plusIndex != -1) {
String before = s.substring(0, plusIndex);
// Use before
}
It's not really clear why your original version didn't work, but then you didn't say what actually happened. If you want to split not using regular expressions, I'd personally use Guava:
Iterable<String> bits = Splitter.on('+').split(s);
String firstPart = Iterables.getFirst(bits, "");
If you're going to use split (either the built-in version or Guava) you don't need to check whether it contains + first - if it doesn't there'll only be one result anyway. Obviously there's a question of efficiency, but it's simpler code:
// Calling split unconditionally
String[] parts = s.split("\\+");
s = parts[0];
Note that writing String[] parts is preferred over String parts[] - it's much more idiomatic Java code.
[+]is simpler
String s = "ddjdjdj+kfkfkf";
if(s.contains ("+"))
{
String parts[] = s.split("[+]");
s = parts[0]; // i want to strip part after +
}
System.out.println(s);

Categories