substring 1 « substring « Java Data Type Q&A





1. How to see if a substring exists inside another string in Java 1.4    stackoverflow.com

How can I tell if the substring "template" exists inside a String in java 1.4 It would be great if it was a non case sensitive check. Thanks!

2. how the subString() function of string class works    stackoverflow.com

Hi please see the following code.

String s = "Monday";
if(s.subString(0,3).equals("Mon"){}

String s2 = new String(s.subString(0,3));
String s3 = s.subString(0,3);
I know that line 2 will still point to "Monday" and have a new String object ...

3. Occurences of substring in a string    stackoverflow.com

Why is the following algorithm not halting for me? (str is the string I am searching in, findStr is the string I am trying to find)

    String str = ...

4. Java and substring    stackoverflow.com

I need something simmilar to "awk { print $1 }", but for java. I have a string, similar to this: word1 word2 word3 word4 word5 word6 How, can I substring first word, from every ...

5. Java parsing many instances of substring from String    stackoverflow.com

I am trying to write a small java program that will accept a file (using Scanner class), return the file as a String, and then search that string for any instance ...

6. In java, how to check if a string contains a substring , ( ignoring the case )?    stackoverflow.com

I have two Strings: str1 and str2. How to check if str2 is contained within str1, ignoring case?

7. Java substring check    stackoverflow.com

I have a String2. I want to check whether String2 exists in String1. String1's length can be less or greater or equal than String2. Also String2 can be ...

8. Java strings and substrings    stackoverflow.com

Can someone help me with this Java problem? I want to write a method that takes two strings as arguments and returns true if each character (including space and punctuation) that appears ...

9. Last substring of string    stackoverflow.com

In java we have indexOf and lastIndexOf. Is there anythink like lastSubstring? It should work like :

"aaple".lastSubstring(0, 1) = "e";





10. Tear subString from within HTML tags with JAVA    stackoverflow.com


let say i have a string like this "neverMind<b>What is up</b>neverMind" and I want to take out the What is up using regexp with JAVA. Someone told me that using matcher ...

11. Dividing a string into substring in JAVA    stackoverflow.com

As per my project I need to devide a string into two parts. below is the example:

String searchFilter = "(first=sam*)(last=joy*)";
Where searchFilter is a string. I want to split above string to two parts first=sam* ...

12. java substring?    stackoverflow.com

I have the following code:

String table;
private DB.ConnectMAS mas=new DB.ConnectMAS();
java.sql.ResultSet rs1 = mas.DBquery("select * from products");


try{ 
String duedate = "";
while(rs1.next()){
    String duedate = rs1.getString("duedate");
table+="<tr><td>"+duedate.substring(1,10)+"</td></tr>"; 
} catch(java.sql.SQLException e){}
It is ...

13. Compare Multiple Substrings    stackoverflow.com

I'm attempting to write a basic dna sequencer. In that, given two sequences of the same length, it will output the strings which are the same, with a minimal length of ...

14. Count of substrings within string    stackoverflow.com

My program should do the following:

  1. User enters a string: University of the Cordilleras
  2. User enters the substring: er
  3. Program outputs the substring-count: 2 (University of the Cordilleras)
I should not use .str, but create ...

15. Parsing a string to a Java method    stackoverflow.com

I'm completely new to Java, so I'm sorry if the answer to this seems obvious... I've written a method in my class as follows:

private static final String[] rCode = {"M", "CM", "D", ...

16. Time complexity of Java's substring()    stackoverflow.com

What is the time complexity of the String#substring() method in Java?





17. Divide a string into two at hyphen    stackoverflow.com

I am taking a String variable from request.

String issueField = request.getParameter("issueno");
This may or may not have a hyphen in the middle. I want to be able to traverse through the String ...

18. Optimal way to obtain substrings?    stackoverflow.com

Is there a more optimal way to obtain subtrings which are separated by ',' and ended by '.' than this way?

if (paramsText != null)
   {
     ...

19. Any Commons all substrings function?    stackoverflow.com

I'm wondering if there's a library function (or why StringUtils in Apache Commons doesn't have it?) that for a String calculates all the substrings. For "abcde" it retuns "a", "ab", "b", ...

20. Longest Duplicated String    stackoverflow.com

Hi I have to write a program that searches for the longest duplicated sub-string in an inputted string The following is the method were the string is inputted from the main ...

21. Java String optimisation    stackoverflow.com

How many Strings does this create?

String test(String text) {
  return "string 1 " +
    text + " string 2 " +
    "string 3";
}

22. Efficient way to Parse String    stackoverflow.com

say for example you have a string that has key values but the variables after it can change ex:

KEY1=variable1, KEY2=variable2, KEY3=variable3
What I want to know is what is the best way to ...

23. Beginner Java - using substring() to make a long string into seperate lines    stackoverflow.com

We are suppose to write a calendar for a project. I'm using an array to keep track of how many days in a month there are and get a long ...

24. Small annoying problem - illegal start of expression    stackoverflow.com

My Problem: When I enter this piece of script in jcreator, it says illegal start of expression for (public String subString). Is there another way to enter a substring?

public String subString;
 ...

25. Java String.substring returning empty string    stackoverflow.com

I'm trying to run the following code

int[] sbox = new int[256];
String inputString = "Thisisanexample";
String sTemp;
char cTmp;
int intLength = inputString.length();

for (a = 0; a <= 255; a++)
{
    sTemp = ...

26. How to work with specific positions of string in Java?    stackoverflow.com

I need to remove some chars at specific positions of a string in Java. Is there something equivalent to std::string.erase() from C++ in Java? If not, could someone show me a quick ...

27. String manipulation, subString() method which modifies the original String    stackoverflow.com

The problem may be silly, but I couldn't find an answer on google. Question : Is there any method in StringBuffer or StringBuilder or String itself, which does the same as subString(int ...

28. Substring in Java    stackoverflow.com

I have a string in format /remove_this/I_want_this_onward/any_number_of_characters.
I want to remove /remove_this and want to get remaining string i.e. /I_want_this_onward/any_number_of_characters.
Ex. Input String /myApp/home/welcome , I want to extract /home/welcome.
What will be the ...

29. Descending sort of substrings by occurence - Java    stackoverflow.com

Lest's say I have string:

 String test= "AA BB CC BB BB CC BB";
What I would like to do is create String array like this:
 String[]{"BB", "CC", "AA"}
Since B occurred 4 ...

30. how to remove a substring from a string java    stackoverflow.com

I have a string that takes an input. I want to delete anything inside the characters '<' and '>'. for example if the string says

"P.S.<!--
BODY
   {
   ...

31. Is it possible to access a substring of a Java String without creating a new String object?    stackoverflow.com

I just want to read it as its own "String" but a view is fine -- I don't need it to be materialized as a separate object. I'd prefer to not ...

32. Checking for a phrase within a string - Java    stackoverflow.com

String serverNamePercentage = "http://&askldjsakldjskadjlsajdklsajdlsjakldsjakldjsakl";
How do i check in that string that it starts with a "http://&"? EDIT: nevermind, i actually phrased my question wrongly. I wanted a method to check if the string ...

33. How can I remove a substring from a given String?    stackoverflow.com

Is there an easy way to remove substring from a given String in Java ? example : "Hello World!" , removing "o" - > "Hell Wrld!"

34. Unsure what I am doing wrong, simple middle substring    stackoverflow.com

Here's the link to what I'm doing, simple practice:
http://codingbat.com/prob/p162477 However, its telling me that when it makes front = false,

        public String ...

35. Why do I get a StringIndexOutOfBoundsException on the second line here?    stackoverflow.com

Why does the following Java code snippet throw a StringIndexOutOfBoundsException on the third line of code?

String str = "1234567890";
String sub1 = str.substring(0, 3);
String sub2 = str.substring(4, 1);
I'd have expected the result ...

36. How to inteligently concat two strings so as to ignore duplicate substring    stackoverflow.com

I want to concatenate user input intelligently so that It removes duplicate substring in following way.

  • uneasy + easyly = uneasyly
  • concat + catalyst = concatalyst
Here is what I am trying to do, ...

37. substring    bytes.com

Thanks for the quick reply. Will I be able to use the parse int method in the same while statement using substring, or will I have to read it seperate?

38. substring    coderanch.com

39. SubString Question    coderanch.com

Hello all, Seprating of a string having a dot operator Eg: String all = "NAME.PHONE.CODE.COUNTRY", How would i go about it... Means i want to get String name = all.subString(???); will be returning the NAME part String phone = all.subString(???); will be returning the PHONE part String code = all.subString(???); will be returning the CODE part String country = all.subString(???); will ...

40. Is This an Optimal Substring Solution???    coderanch.com

I'm new to Java but experienced as a developer. I'm working on a string parser that has to fire every second and scan a full page of text that is rendered as HTML. I'm wondering if I can make it run faster using better code. Here's what I'm using (see code) is there a better/faster way to do this? Would I ...

42. Substring    coderanch.com

hi i have following string -"[ValidationMessage: messageCode:Effective Entry Date in Batch Header cannot be more than 60 days in the future. context:null]" i want to extract "Effective Entry Date in Batch Header cannot be more than 60 days in the future." part from this string what is best way to do it.???

43. substring method    coderanch.com

44. substring    coderanch.com

How does the substring work? for example I want to take a phone number 1234567 and place a "-" after the third digit? I know this is such a simple solution but the books I have do not do a good job in explaining how or give any good examples. Thank you.

45. Java Strings Getting a Substring    coderanch.com

Hi all, I have an address that is coming in the form of 2000-2345 S Campbell Avenue I want to get three substrings : 2000 2345 and S Campbell Avenue Then I want to create 2000 S Campbell Avenue and 2345 S Campbell Avenue How can I do it ? Any sample code,.. Please Also the address can have a space ...

46. substring    coderanch.com

47. how do i get substring?    coderanch.com

48. counting letters in a substring    coderanch.com

Hi GitaLakshmi, Welcome to JavaRanch. Maybe something like this: import java.util.*; public class WordSize { public static int wordSize(String sentence, int wordSize) { int words = 0; StringTokenizer st = new StringTokenizer(sentence); while (st.hasMoreTokens()) { int size = st.nextToken().length(); if (size == wordSize) { words++; } } return words; } public static void main(String[] args) { String sentence = "how do ...

49. substring, strings, and =='s    coderanch.com

50. SubString PROBLEM. HELP ASAP!!!    coderanch.com

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ...

51. subString method in String class    coderanch.com

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ...

52. subString    coderanch.com

class Stringex { public static void main(String args[]) { String s1 = "Hello"; String s2 = "Hello"; String s3 = new String("Hello"); String s4 = new String("Hello"); String s5 = " World"; System.out.println(s1 == s2); System.out.println(s3 == s4); System.out.println(s1 == s3); System.out.println(s2 == s4); System.out.println("***********************"); System.out.println(s1.equals(s2)); System.out.println(s3.equals(s4)); System.out.println(s1.equals(s3)); System.out.println(s2.equals(s4)); System.out.println("***********************"); System.out.println("Concat :"+" "+ s3.concat( s5)); System.out.println("Replace :"+" "+s5.replace('o','a')); [B] System.out.println("SubString:"+" "+s5.subString(1,3));[/B] ...

53. Can I delete a substring?    coderanch.com

be carefull with strings. remember that you can't change a string once it is created. your manipulation will create a NEW string, and unless you assign your reference to it, it will be lost. in other words, if you do something like String myString = new String("fred"); myString.toUpperCase(); System.out.println(myString); the output will be "fred". but if you do myString = myString.toUpperCase(); ...

54. substring    coderanch.com

55. Substring    coderanch.com

I am a beginner in Java. I have a String as below: public class Name { public static void main(String[] args) { String s = "John Doe"; String s1 = "Jane White"; System.out.println(); } } Now I want to print First name and last name seperately. How do I do this? I know I can write code as s.substring(0,4) or vice ...

56. Problem in Substring    coderanch.com

hi first something else... - use code tags to format code you post! - try to stick with the coding guidelines from sun (recv_Message -> recvMessage, Four_characters_Integers -> fourCharacterIntegers and so on). this code is hard to read! the exception does not occur in the line four_characters=recv_Message.substring(recv_Message.length() - 4); since it is an exception that is thrown by Integer.parseInt()! but the ...

57. Problem in using Substring    coderanch.com

58. substring    coderanch.com

59. substring from user input    coderanch.com

** I cant seem to figure out how to incorporate splitting a string from user input. Any help would be appreciated. Next write a Java program that enters a 10-digit string as a typical U.S. telephone number, extracts the 3-digit area code, the 3-digit exchange, and the remaining 4-digit number as separate strings, prints them and then prints the complete telephone ...

60. Help with 'subString and replaceAll'    coderanch.com

Originally posted by Jesper Young: Regular expressions are also a perfect solution for a task like this: I agree that using a regex can easily solve this problem. However, I believe the best approach to use depends on the complexity of the problem and the context in which it is being used. There is also a significant performance overhead associated with ...

61. getting a substring from a string    coderanch.com

63. Substring    coderanch.com

64. substring like function on BigDecimal/int datatype    coderanch.com

This was pretty interesting to do. I'll post my solution tommorow in case he wants to give it a try. Just something to look out for when you do your "substring" method on ints if you do substringInt(20070801, beginIndex, endIndex) and you start with leading zeros for the begin index ...I.E. (zero base indexes) 1,2,4,6 in our example they are gonna ...

65. longest repeated substring - overlaping allowed    coderanch.com

Hello, I have 1MB file with characters representing part of DNA. I need to find longest repeated substring (LRS) but these substrings can be over each other. Example: BANANAS 1) LRS without overlapping: AN or NA 2) LRS with overlapping: ANA I need to find the second one. I wanted to use suffix tree. But if I understood at least a ...

66. Right way to concat Strings and then substring them ?    coderanch.com

/** * @author * */ public class TestingSetforloop { public static void main(String[] args) { String s1 = "abc"; String s2 = "def"; String finalString = s1 + "|" + s2; System.out.println("First string part = " + finalString.substring(0, finalString.indexOf("|"))); System.out.println("Second string part = " + finalString.substring(finalString.indexOf("|") + 1, finalString.length())); } } This code is like a snapshot from problem domain, what ...

68. substring problem    coderanch.com

69. substring problem    coderanch.com

70. A substring question    coderanch.com

I would suggest writing a performance test for it if it's that big of a concern. Run it a million times with the different variations to get the timing. I suspect the first way (or maybe the last way with the ternary operator since it probably gets compiled to the same or very similar code) will be fastest. When trying s.substring(Math.min(i1,i2),Math.max(i1,i2)) ...

71. How to get substring in string line entry?    coderanch.com

The input string is like this: When user enter the following line, change test1.txt test2.txt ... I want to get the word: change and only need the first file name: test1.txt, regardless the test2.txt or others. In other words, for this case of input, I only need "change" and "test1.txt" , no rests. the "change" string as a command, it may ...

72. Removing substrings from strings    coderanch.com

I am having problems with an assignment where I have to remove a substring from a string. I know I have to make a loop to go through the String, use substring() to remove the substring, indexOf() to find the index to end and resume the String, and an if / else statement to tell whether or not to remove the ...

73. Substring    coderanch.com

Hello guys i got this code // Declaracion de variables int i; String b; String c; String d; String e; String str, strNum, strLet; // Iniciando variables str = jTextField1.getText(); strNum = ""; strLet = ""; // Separ numeros de letras for(i = 0; i < 10; i++) { if(Character.isDigit(str.charAt(i))) // Si es numero strNum = strNum + "," + str.charAt(i); ...

74. substring()    coderanch.com

Agreed. The concept of inclusive start and exclusive end, is used in practically every other system that has substring -- and is intuitive to anyone that has ever used substring in any programming language. On the other hand, I guess it can get confusing to someone who is just learning his/her first language. Henry

75. Implementing substring method in my own way.    coderanch.com

class StringEx { public static void main(String args[]) { String splitText = "Hello World Good Morning"; char[] charArray = splitText.toCharArray(); String temp = ""; String[] stringArray = new String[50]; for(int i=0; i

76. Substring capture To Write A list Of instances in A New Document    coderanch.com

Hi Guys, About my Program: Im brand new to Java and this is further development for my first program. So far my program captures the data between two strings and stores the substring value within an array. Once all the documents have been read in from the folder and all instances are captured. They are then sorted within the array and ...

77. Using substring    coderanch.com

I need help with using substring and its two parameters when the string is shorter than the second parameter. Example: String abc = "Steve Dyke" abc.substring(0,253) fails and throws exception because string is not at least 253 characters long. However, in my real situation I will have strings that are longer than 253 characters and some shorter. I need the max ...

78. substring problem    coderanch.com

79. need help with substring    coderanch.com

80. [SOLVED] Substring Method    java-forums.org

81. substring problem    java-forums.org

Yes it's possible. But have to care of the original string length. When you are working on sub-string, you have to define start index and end index. End index is 1 less than length of the string. Start index is the one you want to change. Ok, here you get an index out of range error , means one of the ...

82. Extarctin substring    java-forums.org

Hi everyone I am new in java, I am working on a reseach project that I need extyract some sustring from some huge text file, but my problem is that in each line there are many missing data that is blank(space) and the program that I have writen, give me the : java.lang.StringIndexOutOfBoundsException error when it reach to the first misssing ...

83. How to Use substring and .concat    java-forums.org

Hello. I am new to Java and this forum. I apologize if this post is going to the wrong forum. I would like to concatenate strings Hi and Hello World. This is what I have: // Declare variables String sentence1; String sentence2; //Assign variables sentence1 = "Hi. "; sentence2 = "Hello World"; System.out.println("The index of first w in sentence2 = " ...

84. substring issue    java-forums.org

Hi everyone, I am trying to divide an integer number into several Strings, each of them containing 3 characters. (For example 12345678 should be "123", "456","78")I wrote the following simple code, which seems pretty fine to me. The problem is that I get an Exception at my first substring function. I tried debugger and it seems that the exceptions is caused ...

85. How do I remove a substring from a string?    java-forums.org

One thing you can do to make the code a bit more robust in case the http:// bit is not present is to do an indexOf "//". If it is greater than 0 then indexOf "/" starting from that position (2 parameter method). Else indexOf "/" from the start of the URL (1 parameter method).

86. A string and substring problem    java-forums.org

87. I don't know what I'm doing wrong in my substring.    java-forums.org

/**Determines if the passed in String is made of two words * in a row. For example: "HelloHello" * Precondition str's length is even * @param str The String to be considered * @return The String that comes first using the alphabet. */ public static boolean isADoubleWord(String str) { int len = str.length(); int halfLength = (len/2); if(str.substring(0 , halfLength) == ...

88. Getting substring ???    java-forums.org

89. Using the length() method to obtain a substring...    forums.oracle.com

} } Now, where it says //Method will detect the length of the string, I was trying to use the length() method to detect the length and then use that to allow the program to find the first five letters of the second word. But I keep getting compiling errors for incompatible types. For instance, I would try something like this: ...

90. Substring    forums.oracle.com

91. Substring    forums.oracle.com

Hi, I need help with the following, I have to desing a method that takes 2 strings as parameters and returns a string also. the first string will be a person's last name and second will be thier Social security number. The method should take the first two letters of last name and they must be in lowercase, and concatenatre them ...

92. substring may be null, then what?    forums.oracle.com

I want to view only the first 5 characters of a string. The string variable may be empty. Here are the two ways I've tried to do this: ctyCd is a string variable up to 25 characters long. ONE: if (ctyCd.equals(" ")) { ctyCd = ctyCd.substring(0, 0); } else { ctyCd = ctyCd.substring(0, 5); } TWO: if (ctyCd != null) { ...

93. Substring    forums.oracle.com

94. can I get a substring from my object?    forums.oracle.com

I am gathering my objects, in a third party software, as such: Object objects [] = IL.getSelectedObjects( "WSIP"); It comes along with extra information at the beginning. To get the extra information in the beginning I am using: Window activewindow = ILEventManager.getActiveWindow(); String titleCapture= "null"; titleCapture = activewindow.findComponentAt(10,10).getAccessibleContext().getAccessibleName(); int len = titleCapture.length(); String ws = titleCapture.substring(12,len); len = ws.length(); Can I ...

95. Compare substrings in strings    forums.oracle.com

string.indexOf I was thinking of using the string.charAt basically what I need to do is step into each index of the arrayList and go through the string in that index and look for a substring so instead of what I had written I could write it as this for (int i = 0; i <= ...

96. Compare substrings in strings    forums.oracle.com

97. Duplicate substrings in a string......    forums.oracle.com

On input "seven saints and seven dinners" return "seven ". Note that the answer ends in a space and has length 6 I don't understand why u need a space at the end of the answer/result. I did not include that in the code I wrote. U just have to print a space if you want to show that in the ...

98. Troubles with substring()    forums.oracle.com

99. Effective way to copy substring without generating garbage    forums.oracle.com

Hi, I am trying to copy portion of STRING1 to STRINGBUFFER1. STRINGBUFFER.append(STRING1.substring(from_index, to_index)); However, above method generates a String object, then copy its content to STRINGBUFFER, creating garbage. Is there some way to do this without generating garbage? Of course, I know I can create a loop and copy one character at a time, but I am afraid that will be ...

100. Substring() Issue    forums.oracle.com