Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:59:15+00:00 2026-05-28T06:59:15+00:00

I am an experienced C++ programmer but new to Java. I have an algorithm

  • 0

I am an experienced C++ programmer but new to Java.

I have an algorithm that does some string manipulation and I want to terminate a string in the middle.

Say, I want to do something like this

String str("hello, world");
char[] str2 = str.toCharArray(); 
//pass str2 to some function 
myFunc(str2);
...
//inside myFunc, I decide to terminate str2 at position 3, so I did
//str2[3] = 0

//then it returns, how can I construct an String type variable from str2 that includes chars before the '\0'?
//String str3 = new String(str2), doesn't work

I can write my own C-liked strlen. However I would like to know what is the elegant way of doing it in Java.

Also, it is hard to find document about type char[]. Whenever I Google, I can only find document on “char” instead of “char[]”. I am thinking char[] may have methods liked substr or strlen.

Thanks for any suggestion!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:59:16+00:00Added an answer on May 28, 2026 at 6:59 am

    In Java, char[] and String are completely different. Forget the association from C, As far as you’re concerned for now, they’re not connected in any way. (For completeness, that String objects hold a final char[] representing the string (and it’s not null-terminated), which you can obtain as you did in your example. Don’t do that until you’re clear on the difference.) You call all your methods on the string. But remember, in Java, Strings are immutable – if you have a String, it will always be the same. You can assign a new String object to a variable, but the old string won’t change (until all references are gone and it gets GC’d, of course). This is important to remember when you are trying to get a new string, like this. Once you’ve figured out how long you want your string, use .substring().

    So your example should look something like this:

    // Remember in Java no object is statically allocated.
    String str1 = "Hello, World";
    int indexToCutAt = myFunc(str1);
    // We can't change str1, so we'll need to store the new string.
    String str2 = str1.substring(0,indexToCutAt);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to PHP, but experienced Java programmer in complex enterprise environments
I'm an experienced Java programmer that for the last two years have programmed for
I'm absolutely new to GWT, java and eclipse, but I'm an experienced MS programmer.
I am an experienced C++/Java programmer but am new to iOS. I am writing
I'm new to SoapUI and Groovy, but an experienced Java programmer. I created a
I'm a reasonably experienced Java programmer but relatively new to Java2D. I'm trying to
I'm a pretty experienced Java programmer that's been doing quite a bit of Win32
I am not an experienced Java programmer and i'm trying to write some text
I'm an experienced programmer, but relatively new to SQL. We're using Oracle 10 and
I am an experienced programmer, but new to C. I am trying to learn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.