how to retrieve part of a string in java?

mithun1538 :

I am designing a chat applet. In this, a user will append his name to the beginning of the message that he sends to the other user. In the window of other user, I want to retrieve the appended user name from that string. How do I do that? The message sent by the user is as follows :

final_msg = user_name + ": " + user_message

Hence I want to know how to retrieve the user_name string only. Is there a function that can retrieve a substring upto the first ":"? I dont want to use final_msg.split(":"), because there is a possiblity that the user_message contains ":", which will then give me an array of strings.

ring bearer :

Quick tip:

use indexOf method to find first index of ':' character, then do a substring() call.

String userName = final_msg.substring(0,final_msg.indexOf(':'));

Edit: Consider renaming your variable from final_msg to finalMsg - just because, that is the Java style. Normally "_" appear in Java Constant names

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to read string part in java

How to take part of a string in java?

How to retrieve only particular part of string in SQL Server 2012

How can I get a part of a String in Java?

How to get sub-part of a string in java?

How to get a specific part of a string in java

How to replace a part of email domain string in Java

How to get a part of URL in string in Java

how to extract specific part of a string in java

How to get part of a string in Java and print it?

How to print part of a String from an array (Java)?

How to retrieve the original string from the shuffled string of a Java algorithm

Retrieve Numbers After Certain Part of String

A better way to retrieve a part of string with groovy

Retrieve part of a string from a full text

Remove part of string in Java

replace part of a string in java

How to retrieve a portion of a string?

How to retrieve SharedPreferences string?

JNI on Android: How to retrieve a string from Java code?

How to parse a String containing XML in Java and retrieve the value of the root node?

How to retrieve a property of collection as a collection of string using Java Stream api

How to retrieve string from JTextField in Java to an Action Listener?

How to retrieve Mongo query string from FindIterable (mongo java driver)

Dovecot Sieve - How to retrieve part of message as a variable

How to retrieve a part of a stored procedure's header?

How to retrieve only part of a document back in a call

How Retrieve Part Of text Using Regex Sunstring

How to check if a given string is a part of any given Enum in Java?